Set bookable product base cost programatically in Woocommerce Bookings










2















I am trying to calculate the Base cost of a WooCommerce bookable product and managed to get it done using this:



 function modify_baseprice() 
global $post;
$productid = $post->ID;
$product = new WC_Product($productid);
$product_block_price = $product->wc_booking_block_cost;
$product->wc_booking_cost = ($product_block_price*0.6) + 100;
$pricing_data = update_post_meta( $productid, '_wc_booking_cost', $product->wc_booking_cost);
return $pricing_data;

add_action( 'woocommerce_bookings_after_booking_base_cost', 'modify_baseprice', 10, 3 );


It does calculate the Base cost correctly but I need to refresh the page twice to see it appearing on the Base cost field. Is there a way that I can get it to appear after the first save?



enter image description here










share|improve this question




























    2















    I am trying to calculate the Base cost of a WooCommerce bookable product and managed to get it done using this:



     function modify_baseprice() 
    global $post;
    $productid = $post->ID;
    $product = new WC_Product($productid);
    $product_block_price = $product->wc_booking_block_cost;
    $product->wc_booking_cost = ($product_block_price*0.6) + 100;
    $pricing_data = update_post_meta( $productid, '_wc_booking_cost', $product->wc_booking_cost);
    return $pricing_data;

    add_action( 'woocommerce_bookings_after_booking_base_cost', 'modify_baseprice', 10, 3 );


    It does calculate the Base cost correctly but I need to refresh the page twice to see it appearing on the Base cost field. Is there a way that I can get it to appear after the first save?



    enter image description here










    share|improve this question


























      2












      2








      2








      I am trying to calculate the Base cost of a WooCommerce bookable product and managed to get it done using this:



       function modify_baseprice() 
      global $post;
      $productid = $post->ID;
      $product = new WC_Product($productid);
      $product_block_price = $product->wc_booking_block_cost;
      $product->wc_booking_cost = ($product_block_price*0.6) + 100;
      $pricing_data = update_post_meta( $productid, '_wc_booking_cost', $product->wc_booking_cost);
      return $pricing_data;

      add_action( 'woocommerce_bookings_after_booking_base_cost', 'modify_baseprice', 10, 3 );


      It does calculate the Base cost correctly but I need to refresh the page twice to see it appearing on the Base cost field. Is there a way that I can get it to appear after the first save?



      enter image description here










      share|improve this question
















      I am trying to calculate the Base cost of a WooCommerce bookable product and managed to get it done using this:



       function modify_baseprice() 
      global $post;
      $productid = $post->ID;
      $product = new WC_Product($productid);
      $product_block_price = $product->wc_booking_block_cost;
      $product->wc_booking_cost = ($product_block_price*0.6) + 100;
      $pricing_data = update_post_meta( $productid, '_wc_booking_cost', $product->wc_booking_cost);
      return $pricing_data;

      add_action( 'woocommerce_bookings_after_booking_base_cost', 'modify_baseprice', 10, 3 );


      It does calculate the Base cost correctly but I need to refresh the page twice to see it appearing on the Base cost field. Is there a way that I can get it to appear after the first save?



      enter image description here







      php wordpress woocommerce product woocommerce-bookings






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 20:24









      LoicTheAztec

      91.6k1365105




      91.6k1365105










      asked Nov 15 '18 at 19:35









      PradeepPradeep

      298




      298






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Since Woocommerce 3 release, CRUD objects have been implemented. It's the case of WC_Product object and also for Woocommerce Bookings plugin. So you can use available Getters and setters methods as properties are not anymore accessible (in most cases).



          The following code use this better way (The cost is set in the product without any need of refreshing the page):



          add_action( 'woocommerce_process_product_meta_booking', 'modify_bookable_product_base_cost', 100, 1 );
          function modify_bookable_product_base_cost( $product_id )
          // Get an instance of the WC_Product object
          $product = wc_get_product( $product_id );

          // We check that we have a block cost before
          if ( $product->get_block_cost() > 0 )
          // Calculation
          $new_booking_cost = ( $product->get_block_cost() * 0.6 ) + 100;

          $product->set_cost( $new_booking_cost ); // Set the new calculated cost in the product
          $product->save(); // Save the product data




          Code goes in function.php file of your active child theme (active theme). Tested and works.






          share|improve this answer

























          • Could you kindly explain if we can use something similar to control the other items on Add new product screen? For example I want to make the Bookable item as the default if not the only item for some user roles and hide/remove all non essential options. Since the introduction of CRUD objects certain filters 'default_product_type' for example, no longer working.

            – Pradeep
            Nov 22 '18 at 17:28











          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%2f53326767%2fset-bookable-product-base-cost-programatically-in-woocommerce-bookings%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









          1














          Since Woocommerce 3 release, CRUD objects have been implemented. It's the case of WC_Product object and also for Woocommerce Bookings plugin. So you can use available Getters and setters methods as properties are not anymore accessible (in most cases).



          The following code use this better way (The cost is set in the product without any need of refreshing the page):



          add_action( 'woocommerce_process_product_meta_booking', 'modify_bookable_product_base_cost', 100, 1 );
          function modify_bookable_product_base_cost( $product_id )
          // Get an instance of the WC_Product object
          $product = wc_get_product( $product_id );

          // We check that we have a block cost before
          if ( $product->get_block_cost() > 0 )
          // Calculation
          $new_booking_cost = ( $product->get_block_cost() * 0.6 ) + 100;

          $product->set_cost( $new_booking_cost ); // Set the new calculated cost in the product
          $product->save(); // Save the product data




          Code goes in function.php file of your active child theme (active theme). Tested and works.






          share|improve this answer

























          • Could you kindly explain if we can use something similar to control the other items on Add new product screen? For example I want to make the Bookable item as the default if not the only item for some user roles and hide/remove all non essential options. Since the introduction of CRUD objects certain filters 'default_product_type' for example, no longer working.

            – Pradeep
            Nov 22 '18 at 17:28
















          1














          Since Woocommerce 3 release, CRUD objects have been implemented. It's the case of WC_Product object and also for Woocommerce Bookings plugin. So you can use available Getters and setters methods as properties are not anymore accessible (in most cases).



          The following code use this better way (The cost is set in the product without any need of refreshing the page):



          add_action( 'woocommerce_process_product_meta_booking', 'modify_bookable_product_base_cost', 100, 1 );
          function modify_bookable_product_base_cost( $product_id )
          // Get an instance of the WC_Product object
          $product = wc_get_product( $product_id );

          // We check that we have a block cost before
          if ( $product->get_block_cost() > 0 )
          // Calculation
          $new_booking_cost = ( $product->get_block_cost() * 0.6 ) + 100;

          $product->set_cost( $new_booking_cost ); // Set the new calculated cost in the product
          $product->save(); // Save the product data




          Code goes in function.php file of your active child theme (active theme). Tested and works.






          share|improve this answer

























          • Could you kindly explain if we can use something similar to control the other items on Add new product screen? For example I want to make the Bookable item as the default if not the only item for some user roles and hide/remove all non essential options. Since the introduction of CRUD objects certain filters 'default_product_type' for example, no longer working.

            – Pradeep
            Nov 22 '18 at 17:28














          1












          1








          1







          Since Woocommerce 3 release, CRUD objects have been implemented. It's the case of WC_Product object and also for Woocommerce Bookings plugin. So you can use available Getters and setters methods as properties are not anymore accessible (in most cases).



          The following code use this better way (The cost is set in the product without any need of refreshing the page):



          add_action( 'woocommerce_process_product_meta_booking', 'modify_bookable_product_base_cost', 100, 1 );
          function modify_bookable_product_base_cost( $product_id )
          // Get an instance of the WC_Product object
          $product = wc_get_product( $product_id );

          // We check that we have a block cost before
          if ( $product->get_block_cost() > 0 )
          // Calculation
          $new_booking_cost = ( $product->get_block_cost() * 0.6 ) + 100;

          $product->set_cost( $new_booking_cost ); // Set the new calculated cost in the product
          $product->save(); // Save the product data




          Code goes in function.php file of your active child theme (active theme). Tested and works.






          share|improve this answer















          Since Woocommerce 3 release, CRUD objects have been implemented. It's the case of WC_Product object and also for Woocommerce Bookings plugin. So you can use available Getters and setters methods as properties are not anymore accessible (in most cases).



          The following code use this better way (The cost is set in the product without any need of refreshing the page):



          add_action( 'woocommerce_process_product_meta_booking', 'modify_bookable_product_base_cost', 100, 1 );
          function modify_bookable_product_base_cost( $product_id )
          // Get an instance of the WC_Product object
          $product = wc_get_product( $product_id );

          // We check that we have a block cost before
          if ( $product->get_block_cost() > 0 )
          // Calculation
          $new_booking_cost = ( $product->get_block_cost() * 0.6 ) + 100;

          $product->set_cost( $new_booking_cost ); // Set the new calculated cost in the product
          $product->save(); // Save the product data




          Code goes in function.php file of your active child theme (active theme). Tested and works.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 20:42

























          answered Nov 15 '18 at 20:15









          LoicTheAztecLoicTheAztec

          91.6k1365105




          91.6k1365105












          • Could you kindly explain if we can use something similar to control the other items on Add new product screen? For example I want to make the Bookable item as the default if not the only item for some user roles and hide/remove all non essential options. Since the introduction of CRUD objects certain filters 'default_product_type' for example, no longer working.

            – Pradeep
            Nov 22 '18 at 17:28


















          • Could you kindly explain if we can use something similar to control the other items on Add new product screen? For example I want to make the Bookable item as the default if not the only item for some user roles and hide/remove all non essential options. Since the introduction of CRUD objects certain filters 'default_product_type' for example, no longer working.

            – Pradeep
            Nov 22 '18 at 17:28

















          Could you kindly explain if we can use something similar to control the other items on Add new product screen? For example I want to make the Bookable item as the default if not the only item for some user roles and hide/remove all non essential options. Since the introduction of CRUD objects certain filters 'default_product_type' for example, no longer working.

          – Pradeep
          Nov 22 '18 at 17:28






          Could you kindly explain if we can use something similar to control the other items on Add new product screen? For example I want to make the Bookable item as the default if not the only item for some user roles and hide/remove all non essential options. Since the introduction of CRUD objects certain filters 'default_product_type' for example, no longer working.

          – Pradeep
          Nov 22 '18 at 17:28




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326767%2fset-bookable-product-base-cost-programatically-in-woocommerce-bookings%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







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線