Wordpress/WC - Update Post/Product Meta Before Page loads










0














I am trying to create the functionality in my WooCommerce store to update the stock quantity of items as they are clicked on / their product page is loaded based on an external database and API. I have successfully created the function using a hook like this:



add_action('woocommerce_before_single_product', 'update_product_stock');
function update_product_stock()
global $product;
$sku = $product -> get_sku();
//code for updating based on $sku



So, when the page for a product loads, this correctly changes the stock amount for the product. The problem is, this change is not reflected as the page is loaded/rendered. The page must be refreshed or re-visited for the new stock amount to be shown. I have also tried to use the 'init' hook and the 'template_redirect' hook, but these do not allow me access to to the product to get the id/sku/other info to send to the the API for data retrieval.



Does anyone know of a way for me to get the item details of a product, update the post meta (I am using wc_update_product_stock()), and have those changes reflected on a page view without having to reload? I think I will eventually also have to implement this on a search results page, but I want to get this sorted first.










share|improve this question


























    0














    I am trying to create the functionality in my WooCommerce store to update the stock quantity of items as they are clicked on / their product page is loaded based on an external database and API. I have successfully created the function using a hook like this:



    add_action('woocommerce_before_single_product', 'update_product_stock');
    function update_product_stock()
    global $product;
    $sku = $product -> get_sku();
    //code for updating based on $sku



    So, when the page for a product loads, this correctly changes the stock amount for the product. The problem is, this change is not reflected as the page is loaded/rendered. The page must be refreshed or re-visited for the new stock amount to be shown. I have also tried to use the 'init' hook and the 'template_redirect' hook, but these do not allow me access to to the product to get the id/sku/other info to send to the the API for data retrieval.



    Does anyone know of a way for me to get the item details of a product, update the post meta (I am using wc_update_product_stock()), and have those changes reflected on a page view without having to reload? I think I will eventually also have to implement this on a search results page, but I want to get this sorted first.










    share|improve this question
























      0












      0








      0







      I am trying to create the functionality in my WooCommerce store to update the stock quantity of items as they are clicked on / their product page is loaded based on an external database and API. I have successfully created the function using a hook like this:



      add_action('woocommerce_before_single_product', 'update_product_stock');
      function update_product_stock()
      global $product;
      $sku = $product -> get_sku();
      //code for updating based on $sku



      So, when the page for a product loads, this correctly changes the stock amount for the product. The problem is, this change is not reflected as the page is loaded/rendered. The page must be refreshed or re-visited for the new stock amount to be shown. I have also tried to use the 'init' hook and the 'template_redirect' hook, but these do not allow me access to to the product to get the id/sku/other info to send to the the API for data retrieval.



      Does anyone know of a way for me to get the item details of a product, update the post meta (I am using wc_update_product_stock()), and have those changes reflected on a page view without having to reload? I think I will eventually also have to implement this on a search results page, but I want to get this sorted first.










      share|improve this question













      I am trying to create the functionality in my WooCommerce store to update the stock quantity of items as they are clicked on / their product page is loaded based on an external database and API. I have successfully created the function using a hook like this:



      add_action('woocommerce_before_single_product', 'update_product_stock');
      function update_product_stock()
      global $product;
      $sku = $product -> get_sku();
      //code for updating based on $sku



      So, when the page for a product loads, this correctly changes the stock amount for the product. The problem is, this change is not reflected as the page is loaded/rendered. The page must be refreshed or re-visited for the new stock amount to be shown. I have also tried to use the 'init' hook and the 'template_redirect' hook, but these do not allow me access to to the product to get the id/sku/other info to send to the the API for data retrieval.



      Does anyone know of a way for me to get the item details of a product, update the post meta (I am using wc_update_product_stock()), and have those changes reflected on a page view without having to reload? I think I will eventually also have to implement this on a search results page, but I want to get this sorted first.







      php wordpress hook action






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 15:15









      Case Silva

      499




      499






















          1 Answer
          1






          active

          oldest

          votes


















          0














          I was ultimately able to get the desired functionality that I'll demonstrate here if anyone else ends up needing something similar.



          //this hook runs before the page/product loop
          add_action('storefront_before_site', 'update_product_stock');

          function update_product_stock()
          //since this now runs before the standard post loop,
          //need a different way to access the post.
          //use page id to differentiate between products and other shop pages.
          $page_id = get_queried_object_id();

          //my code for fetching stock here
          $stock_num_to_set = determine_new_stock();

          $product = new WC_Product($page_id);

          //check that the page is indeed a product page
          //(i use the sku, use your own preferred method)
          $product_sku = get_post_meta($page_id, '_sku', true);

          if($product_sku)
          //update using woocommerce product update.
          //now this is the info that will be fetched as the post is loaded normally
          wc_update_product_stock($product, $stock_num_to_set);




          Thanks to this user for their info on a similar question: https://stackoverflow.com/a/3127776/6581190






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265072%2fwordpress-wc-update-post-product-meta-before-page-loads%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I was ultimately able to get the desired functionality that I'll demonstrate here if anyone else ends up needing something similar.



            //this hook runs before the page/product loop
            add_action('storefront_before_site', 'update_product_stock');

            function update_product_stock()
            //since this now runs before the standard post loop,
            //need a different way to access the post.
            //use page id to differentiate between products and other shop pages.
            $page_id = get_queried_object_id();

            //my code for fetching stock here
            $stock_num_to_set = determine_new_stock();

            $product = new WC_Product($page_id);

            //check that the page is indeed a product page
            //(i use the sku, use your own preferred method)
            $product_sku = get_post_meta($page_id, '_sku', true);

            if($product_sku)
            //update using woocommerce product update.
            //now this is the info that will be fetched as the post is loaded normally
            wc_update_product_stock($product, $stock_num_to_set);




            Thanks to this user for their info on a similar question: https://stackoverflow.com/a/3127776/6581190






            share|improve this answer



























              0














              I was ultimately able to get the desired functionality that I'll demonstrate here if anyone else ends up needing something similar.



              //this hook runs before the page/product loop
              add_action('storefront_before_site', 'update_product_stock');

              function update_product_stock()
              //since this now runs before the standard post loop,
              //need a different way to access the post.
              //use page id to differentiate between products and other shop pages.
              $page_id = get_queried_object_id();

              //my code for fetching stock here
              $stock_num_to_set = determine_new_stock();

              $product = new WC_Product($page_id);

              //check that the page is indeed a product page
              //(i use the sku, use your own preferred method)
              $product_sku = get_post_meta($page_id, '_sku', true);

              if($product_sku)
              //update using woocommerce product update.
              //now this is the info that will be fetched as the post is loaded normally
              wc_update_product_stock($product, $stock_num_to_set);




              Thanks to this user for their info on a similar question: https://stackoverflow.com/a/3127776/6581190






              share|improve this answer

























                0












                0








                0






                I was ultimately able to get the desired functionality that I'll demonstrate here if anyone else ends up needing something similar.



                //this hook runs before the page/product loop
                add_action('storefront_before_site', 'update_product_stock');

                function update_product_stock()
                //since this now runs before the standard post loop,
                //need a different way to access the post.
                //use page id to differentiate between products and other shop pages.
                $page_id = get_queried_object_id();

                //my code for fetching stock here
                $stock_num_to_set = determine_new_stock();

                $product = new WC_Product($page_id);

                //check that the page is indeed a product page
                //(i use the sku, use your own preferred method)
                $product_sku = get_post_meta($page_id, '_sku', true);

                if($product_sku)
                //update using woocommerce product update.
                //now this is the info that will be fetched as the post is loaded normally
                wc_update_product_stock($product, $stock_num_to_set);




                Thanks to this user for their info on a similar question: https://stackoverflow.com/a/3127776/6581190






                share|improve this answer














                I was ultimately able to get the desired functionality that I'll demonstrate here if anyone else ends up needing something similar.



                //this hook runs before the page/product loop
                add_action('storefront_before_site', 'update_product_stock');

                function update_product_stock()
                //since this now runs before the standard post loop,
                //need a different way to access the post.
                //use page id to differentiate between products and other shop pages.
                $page_id = get_queried_object_id();

                //my code for fetching stock here
                $stock_num_to_set = determine_new_stock();

                $product = new WC_Product($page_id);

                //check that the page is indeed a product page
                //(i use the sku, use your own preferred method)
                $product_sku = get_post_meta($page_id, '_sku', true);

                if($product_sku)
                //update using woocommerce product update.
                //now this is the info that will be fetched as the post is loaded normally
                wc_update_product_stock($product, $stock_num_to_set);




                Thanks to this user for their info on a similar question: https://stackoverflow.com/a/3127776/6581190







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 at 14:21

























                answered Nov 12 at 20:07









                Case Silva

                499




                499



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265072%2fwordpress-wc-update-post-product-meta-before-page-loads%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto