Add a delivery time based on custom fields calculations for WooCommerce Flat rate Shipping methodWoocommerce “No Shipping methods” message: Customise message based on the Zipcode customer entersCustom delivery range wrong data on WooCommerce Order received pageUnsetting conditionally custom checkout fields in WooCommerceAdd text to order summary based on a custom field value in WooCommerceDisplay custom backorder text from a custom field in Woocommerce cart itemsProgressive quantity based shipping costs with a max cost in WoocommerceWoocommerce add costum field to order meta and admin orderChoosing a date and time after choosing the WooCommerce delivery methodDisplay custom fields values in WooCommerce order and email notification

Source for "everyone has a specific area of Torah that they're naturally drawn to"

Why is DC so, so, so Democratic?

Can someone clarify when a Trigger executes on a single record vs. multiple in one context?

Do I care if the housing market has gone up or down, if I'm moving from one house to another?

Importance of moon phases for Apollo missions

What is the metal bit in the front of this propeller spinner?

My current job follows "worst practices". How can I talk about my experience in an interview without giving off red flags?

Quickest way to move a line in a text file before another line in a text file?

Stellen - Putting, or putting away?

How to deal with making design decisions

I am a dual citizen of United States and Mexico, can I use my Mexican license in california when visiting?

Cargo capacity of a kayak

As a DM of a 4-player group, would it be appropriate for me to run a private 1-on-1 session so that one PC can act secretly?

As the Ferris wheel turns

What does the following chess proverb mean: "Chess is a sea where a gnat may drink from and an elephant may bathe in."

Book in which the "mountain" in the distance was a hole in the flat world

Host telling me to cancel my booking in exchange for a discount?

How often should alkaline batteries be checked when they are in a device?

How can I deal with someone that wants to kill something that isn't supposed to be killed?

Can "Taking algebraic closure" be made into a functor?

Count the identical pairs in two lists

Reissue US, UK, Canada visas in stolen passports

Killing a star safely

Acoustic guitar chords' positions vs those of a Bass guitar



Add a delivery time based on custom fields calculations for WooCommerce Flat rate Shipping method


Woocommerce “No Shipping methods” message: Customise message based on the Zipcode customer entersCustom delivery range wrong data on WooCommerce Order received pageUnsetting conditionally custom checkout fields in WooCommerceAdd text to order summary based on a custom field value in WooCommerceDisplay custom backorder text from a custom field in Woocommerce cart itemsProgressive quantity based shipping costs with a max cost in WoocommerceWoocommerce add costum field to order meta and admin orderChoosing a date and time after choosing the WooCommerce delivery methodDisplay custom fields values in WooCommerce order and email notification






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I use code that displays a custom field on the product edit page. This text box shows the cooking time on the single product page.



Here is the code:



// Backend: Display additional product fields
add_action( 'woocommerce_product_options_general_product_data', 'add_time_field_general_product_data' );

function add_time_field_general_product_data()
// Custom Time Field
woocommerce_wp_text_input( array(
'id' => '_custom_time',
'label' => __( 'Time for cooking', 'woocommerce' ),
));


// Backend: Save the data value from the custom fields
add_action( 'woocommerce_admin_process_product_object', 'save_time_custom_fields_values' );

function save_time_custom_fields_values( $product )
// Save Custom Time Field
if( isset( $_POST['_custom_time'] ) )
$product->update_meta_data( '_custom_time', sanitize_text_field( $_POST['_custom_time'] ) );



// Display custom fields values under item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_time_field_checkout_item_name', 10, 3 );

function custom_time_field_checkout_item_name( $item_qty, $cart_item, $cart_item_key )
if( $value4 = $cart_item['data']->get_meta('_custom_time') )
$item_qty .= '<br /><div class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</div>';


return $item_qty;


// Display custom fields values on orders and email notifications
add_filter( 'woocommerce_order_item_name', 'custom_time_field_order_item_name', 10, 2 );
function custom_time_field_order_item_name( $item_name, $item )
$product = $item->get_product();

if( $value4 = $product->get_meta('_custom_time') )
$item_name .= '<br /><span class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</span>';


return $item_name;



How can get the following functionality based on this code?



For example, a customer adds several dishes to a cart and checkout order. He sees how much time will be cooking this or that dish.



But when the customer chooses the delivery by courier (Flat Rate), in the same block, the delivery time is shown.



Flat Rate = $10



Delivery time = (the longest cooking time is selected from the order) min. + 45 min.



As I understand it, need to get the data of the custom field '_custom_time' when placing the order. Then, somehow need to get the highest value of this field and add 45 minutes.



I ask for your help! I hope that the answer to this question will be useful to many developers.










share|improve this question






























    3















    I use code that displays a custom field on the product edit page. This text box shows the cooking time on the single product page.



    Here is the code:



    // Backend: Display additional product fields
    add_action( 'woocommerce_product_options_general_product_data', 'add_time_field_general_product_data' );

    function add_time_field_general_product_data()
    // Custom Time Field
    woocommerce_wp_text_input( array(
    'id' => '_custom_time',
    'label' => __( 'Time for cooking', 'woocommerce' ),
    ));


    // Backend: Save the data value from the custom fields
    add_action( 'woocommerce_admin_process_product_object', 'save_time_custom_fields_values' );

    function save_time_custom_fields_values( $product )
    // Save Custom Time Field
    if( isset( $_POST['_custom_time'] ) )
    $product->update_meta_data( '_custom_time', sanitize_text_field( $_POST['_custom_time'] ) );



    // Display custom fields values under item name in checkout
    add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_time_field_checkout_item_name', 10, 3 );

    function custom_time_field_checkout_item_name( $item_qty, $cart_item, $cart_item_key )
    if( $value4 = $cart_item['data']->get_meta('_custom_time') )
    $item_qty .= '<br /><div class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</div>';


    return $item_qty;


    // Display custom fields values on orders and email notifications
    add_filter( 'woocommerce_order_item_name', 'custom_time_field_order_item_name', 10, 2 );
    function custom_time_field_order_item_name( $item_name, $item )
    $product = $item->get_product();

    if( $value4 = $product->get_meta('_custom_time') )
    $item_name .= '<br /><span class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</span>';


    return $item_name;



    How can get the following functionality based on this code?



    For example, a customer adds several dishes to a cart and checkout order. He sees how much time will be cooking this or that dish.



    But when the customer chooses the delivery by courier (Flat Rate), in the same block, the delivery time is shown.



    Flat Rate = $10



    Delivery time = (the longest cooking time is selected from the order) min. + 45 min.



    As I understand it, need to get the data of the custom field '_custom_time' when placing the order. Then, somehow need to get the highest value of this field and add 45 minutes.



    I ask for your help! I hope that the answer to this question will be useful to many developers.










    share|improve this question


























      3












      3








      3








      I use code that displays a custom field on the product edit page. This text box shows the cooking time on the single product page.



      Here is the code:



      // Backend: Display additional product fields
      add_action( 'woocommerce_product_options_general_product_data', 'add_time_field_general_product_data' );

      function add_time_field_general_product_data()
      // Custom Time Field
      woocommerce_wp_text_input( array(
      'id' => '_custom_time',
      'label' => __( 'Time for cooking', 'woocommerce' ),
      ));


      // Backend: Save the data value from the custom fields
      add_action( 'woocommerce_admin_process_product_object', 'save_time_custom_fields_values' );

      function save_time_custom_fields_values( $product )
      // Save Custom Time Field
      if( isset( $_POST['_custom_time'] ) )
      $product->update_meta_data( '_custom_time', sanitize_text_field( $_POST['_custom_time'] ) );



      // Display custom fields values under item name in checkout
      add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_time_field_checkout_item_name', 10, 3 );

      function custom_time_field_checkout_item_name( $item_qty, $cart_item, $cart_item_key )
      if( $value4 = $cart_item['data']->get_meta('_custom_time') )
      $item_qty .= '<br /><div class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</div>';


      return $item_qty;


      // Display custom fields values on orders and email notifications
      add_filter( 'woocommerce_order_item_name', 'custom_time_field_order_item_name', 10, 2 );
      function custom_time_field_order_item_name( $item_name, $item )
      $product = $item->get_product();

      if( $value4 = $product->get_meta('_custom_time') )
      $item_name .= '<br /><span class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</span>';


      return $item_name;



      How can get the following functionality based on this code?



      For example, a customer adds several dishes to a cart and checkout order. He sees how much time will be cooking this or that dish.



      But when the customer chooses the delivery by courier (Flat Rate), in the same block, the delivery time is shown.



      Flat Rate = $10



      Delivery time = (the longest cooking time is selected from the order) min. + 45 min.



      As I understand it, need to get the data of the custom field '_custom_time' when placing the order. Then, somehow need to get the highest value of this field and add 45 minutes.



      I ask for your help! I hope that the answer to this question will be useful to many developers.










      share|improve this question
















      I use code that displays a custom field on the product edit page. This text box shows the cooking time on the single product page.



      Here is the code:



      // Backend: Display additional product fields
      add_action( 'woocommerce_product_options_general_product_data', 'add_time_field_general_product_data' );

      function add_time_field_general_product_data()
      // Custom Time Field
      woocommerce_wp_text_input( array(
      'id' => '_custom_time',
      'label' => __( 'Time for cooking', 'woocommerce' ),
      ));


      // Backend: Save the data value from the custom fields
      add_action( 'woocommerce_admin_process_product_object', 'save_time_custom_fields_values' );

      function save_time_custom_fields_values( $product )
      // Save Custom Time Field
      if( isset( $_POST['_custom_time'] ) )
      $product->update_meta_data( '_custom_time', sanitize_text_field( $_POST['_custom_time'] ) );



      // Display custom fields values under item name in checkout
      add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_time_field_checkout_item_name', 10, 3 );

      function custom_time_field_checkout_item_name( $item_qty, $cart_item, $cart_item_key )
      if( $value4 = $cart_item['data']->get_meta('_custom_time') )
      $item_qty .= '<br /><div class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</div>';


      return $item_qty;


      // Display custom fields values on orders and email notifications
      add_filter( 'woocommerce_order_item_name', 'custom_time_field_order_item_name', 10, 2 );
      function custom_time_field_order_item_name( $item_name, $item )
      $product = $item->get_product();

      if( $value4 = $product->get_meta('_custom_time') )
      $item_name .= '<br /><span class="my-custom-style"><strong>' . __("Time for cooking", "woocommerce") . ':</strong> ' . $value4 . ' min.</span>';


      return $item_name;



      How can get the following functionality based on this code?



      For example, a customer adds several dishes to a cart and checkout order. He sees how much time will be cooking this or that dish.



      But when the customer chooses the delivery by courier (Flat Rate), in the same block, the delivery time is shown.



      Flat Rate = $10



      Delivery time = (the longest cooking time is selected from the order) min. + 45 min.



      As I understand it, need to get the data of the custom field '_custom_time' when placing the order. Then, somehow need to get the highest value of this field and add 45 minutes.



      I ask for your help! I hope that the answer to this question will be useful to many developers.







      php wordpress woocommerce checkout shipping-method






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 26 at 15:14









      LoicTheAztec

      106k15 gold badges87 silver badges127 bronze badges




      106k15 gold badges87 silver badges127 bronze badges










      asked Mar 26 at 13:16









      DmitryDmitry

      4510 bronze badges




      4510 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          3














          Try the following codethat will display a delivery time (calculated from the highest item cooking time value + 45 minutes) when a "flat rate" shipping method is selected on checkout page:



          add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate_callback', 10, 2 );
          function action_after_shipping_rate_callback( $method, $index )
          $chosen_shipping_id = WC()->session->get( 'chosen_shipping_methods' )[$index];

          if( is_checkout() && $method->method_id === 'flat_rate' && $method->id === $chosen_shipping_id )
          $extra_time = 45; // Additional time to be added
          $data_array = []; // Initializing

          // Loop through car items
          foreach ( WC()->cart->get_cart() as $cart_item )
          if( $cooking_time = $cart_item['data']->get_meta('_custom_time') )
          $data_array[] = (int) $cooking_time;



          if ( sizeof($data_array) )
          $max_time = (int) max($data_array);
          $delivery_time = $max_time + $extra_time;
          echo '<br><small style="margin-left:2em;border:solid 1px #ccc;padding:2px 5px;"><strong>' . __("Delivery time", "woocommerce") . '</strong>: ' . $delivery_time . ' min.</small>';





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




          To enable that in cart page too, remove is_checkout() && from the IF statement.




          enter image description here




          enter image description here






          share|improve this answer

























          • Thank you so much for the help!

            – Dmitry
            Mar 26 at 16:41










          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%2f55358124%2fadd-a-delivery-time-based-on-custom-fields-calculations-for-woocommerce-flat-rat%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









          3














          Try the following codethat will display a delivery time (calculated from the highest item cooking time value + 45 minutes) when a "flat rate" shipping method is selected on checkout page:



          add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate_callback', 10, 2 );
          function action_after_shipping_rate_callback( $method, $index )
          $chosen_shipping_id = WC()->session->get( 'chosen_shipping_methods' )[$index];

          if( is_checkout() && $method->method_id === 'flat_rate' && $method->id === $chosen_shipping_id )
          $extra_time = 45; // Additional time to be added
          $data_array = []; // Initializing

          // Loop through car items
          foreach ( WC()->cart->get_cart() as $cart_item )
          if( $cooking_time = $cart_item['data']->get_meta('_custom_time') )
          $data_array[] = (int) $cooking_time;



          if ( sizeof($data_array) )
          $max_time = (int) max($data_array);
          $delivery_time = $max_time + $extra_time;
          echo '<br><small style="margin-left:2em;border:solid 1px #ccc;padding:2px 5px;"><strong>' . __("Delivery time", "woocommerce") . '</strong>: ' . $delivery_time . ' min.</small>';





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




          To enable that in cart page too, remove is_checkout() && from the IF statement.




          enter image description here




          enter image description here






          share|improve this answer

























          • Thank you so much for the help!

            – Dmitry
            Mar 26 at 16:41















          3














          Try the following codethat will display a delivery time (calculated from the highest item cooking time value + 45 minutes) when a "flat rate" shipping method is selected on checkout page:



          add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate_callback', 10, 2 );
          function action_after_shipping_rate_callback( $method, $index )
          $chosen_shipping_id = WC()->session->get( 'chosen_shipping_methods' )[$index];

          if( is_checkout() && $method->method_id === 'flat_rate' && $method->id === $chosen_shipping_id )
          $extra_time = 45; // Additional time to be added
          $data_array = []; // Initializing

          // Loop through car items
          foreach ( WC()->cart->get_cart() as $cart_item )
          if( $cooking_time = $cart_item['data']->get_meta('_custom_time') )
          $data_array[] = (int) $cooking_time;



          if ( sizeof($data_array) )
          $max_time = (int) max($data_array);
          $delivery_time = $max_time + $extra_time;
          echo '<br><small style="margin-left:2em;border:solid 1px #ccc;padding:2px 5px;"><strong>' . __("Delivery time", "woocommerce") . '</strong>: ' . $delivery_time . ' min.</small>';





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




          To enable that in cart page too, remove is_checkout() && from the IF statement.




          enter image description here




          enter image description here






          share|improve this answer

























          • Thank you so much for the help!

            – Dmitry
            Mar 26 at 16:41













          3












          3








          3







          Try the following codethat will display a delivery time (calculated from the highest item cooking time value + 45 minutes) when a "flat rate" shipping method is selected on checkout page:



          add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate_callback', 10, 2 );
          function action_after_shipping_rate_callback( $method, $index )
          $chosen_shipping_id = WC()->session->get( 'chosen_shipping_methods' )[$index];

          if( is_checkout() && $method->method_id === 'flat_rate' && $method->id === $chosen_shipping_id )
          $extra_time = 45; // Additional time to be added
          $data_array = []; // Initializing

          // Loop through car items
          foreach ( WC()->cart->get_cart() as $cart_item )
          if( $cooking_time = $cart_item['data']->get_meta('_custom_time') )
          $data_array[] = (int) $cooking_time;



          if ( sizeof($data_array) )
          $max_time = (int) max($data_array);
          $delivery_time = $max_time + $extra_time;
          echo '<br><small style="margin-left:2em;border:solid 1px #ccc;padding:2px 5px;"><strong>' . __("Delivery time", "woocommerce") . '</strong>: ' . $delivery_time . ' min.</small>';





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




          To enable that in cart page too, remove is_checkout() && from the IF statement.




          enter image description here




          enter image description here






          share|improve this answer















          Try the following codethat will display a delivery time (calculated from the highest item cooking time value + 45 minutes) when a "flat rate" shipping method is selected on checkout page:



          add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate_callback', 10, 2 );
          function action_after_shipping_rate_callback( $method, $index )
          $chosen_shipping_id = WC()->session->get( 'chosen_shipping_methods' )[$index];

          if( is_checkout() && $method->method_id === 'flat_rate' && $method->id === $chosen_shipping_id )
          $extra_time = 45; // Additional time to be added
          $data_array = []; // Initializing

          // Loop through car items
          foreach ( WC()->cart->get_cart() as $cart_item )
          if( $cooking_time = $cart_item['data']->get_meta('_custom_time') )
          $data_array[] = (int) $cooking_time;



          if ( sizeof($data_array) )
          $max_time = (int) max($data_array);
          $delivery_time = $max_time + $extra_time;
          echo '<br><small style="margin-left:2em;border:solid 1px #ccc;padding:2px 5px;"><strong>' . __("Delivery time", "woocommerce") . '</strong>: ' . $delivery_time . ' min.</small>';





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




          To enable that in cart page too, remove is_checkout() && from the IF statement.




          enter image description here




          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 26 at 15:16

























          answered Mar 26 at 15:09









          LoicTheAztecLoicTheAztec

          106k15 gold badges87 silver badges127 bronze badges




          106k15 gold badges87 silver badges127 bronze badges












          • Thank you so much for the help!

            – Dmitry
            Mar 26 at 16:41

















          • Thank you so much for the help!

            – Dmitry
            Mar 26 at 16:41
















          Thank you so much for the help!

          – Dmitry
          Mar 26 at 16:41





          Thank you so much for the help!

          – Dmitry
          Mar 26 at 16:41








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f55358124%2fadd-a-delivery-time-based-on-custom-fields-calculations-for-woocommerce-flat-rat%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현