WooCommerce: Show Dispatch / Est. Shipping Date @ Single Product

A good way to inform online customers and avoid issues is showing the estimated delivery / dispatch time on the single product page, just below the “Add to Cart” button. Yes, you could do that manually by adding shipping info to each product short description, but the goal of Business Bloomer is to learn how to code that instead, so you won’t need to write things manually.

Also, this is great because if you change something in your dispatch rules, you just need to change the short PHP snippet and not all your product descriptions. It’s much more flexible this way.

Finally, in this post we’ll learn how to work with cut-off times (hour of the day) and current day of the week (pure PHP), so that we can show a “dynamic” notice based on current date. So, let’s see how it’s done!

It’s Monday before 4PM – this snippet is printing a notice just below the single product page add to cart that if I order by 4PM the product will be shipped today!

PHP Snippet: Display Dispatch / Estimated Delivery Date @ Single Product Page

Case scenario:

  • Friday/Saturday/Sunday orders ship on Monday
  • For other days, if before 4PM ships today…
  • …if after 4PM ships tomorrow

Please note the “date(‘N’)” and the “date(‘H’)” functions, which in PHP they respectively give me the current day of the week and current hour of the day so I can compare them with local & current time. Also look into “date_default_timezone_set()” function in case you want to set a different timezone, which is vital for this snippet’s calculations.

/**
 * @snippet       Dispatch Date @ WooCommerce Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.9
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_after_add_to_cart_form', 'bbloomer_dispatch_info_single_product' );
   
function bbloomer_dispatch_info_single_product() {
	date_default_timezone_set( 'Europe/London' );  
	
	// if FRI/SAT/SUN delivery will be MON
	if ( date( 'N' ) >= 5 ) {
		$del_day = date( "l jS F", strtotime( "next monday" ) );
		$order_by = "Monday";
	} 
	
	// if MON/THU after 4PM delivery will be TOMORROW
	elseif ( date( 'H' ) >= 16 ) {
		$del_day = date( "l jS F", strtotime( "tomorrow" ) );
		$order_by = "tomorrow";
	} 
	
	// if MON/THU before 4PM delivery will be TODAY
	else {
		$del_day = date( "l jS F", strtotime( "today" ) );
		$order_by = "today";
	}

	$html = "<br><div class='woocommerce-message' style='clear:both'>Order by 4PM {$order_by} for delivery on {$del_day}</div>";
	
	echo $html;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

30 thoughts on “WooCommerce: Show Dispatch / Est. Shipping Date @ Single Product

  1. Hi Rodolfo Melogli,

    Thanks for the nice code.
    Can we configure it for multi-regions, like America, London, South Africa, etc? I mean different estimations based on shipping address from different regions of the world?

    1. Hello Rauf, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hi, Rodolfo, I would like to use your snippet in German… I read the Spanish comments, but could not figure out to get a German text on my product pages… Thank you!

    1. Hi Jens! Did you try change the “Order by 4PM {$order_by} for delivery on {$del_day}” string?

  3. How do I show the dates in Spanish?

    1. You mean the date format? Check https://www.php.net/manual/en/function.date.php

  4. how do i change the color of the box from green to possibly black or something?

    1. Hi Victor, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  5. What about caching? A JS implementation would be cache-friendly

    1. Not sure I understand

  6. great! i was looking for something like this. thank you.
    unfortunately the message is also displayed if the product is not in stock. how could i solve this?

    1. Lapos, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. This fragment is fantastic, but it hurts, it does not work for Spain because it cannot be translated.
    You can take a look here:

    To format dates in other languages, you must use the setlocale () and strftime () functions instead of date ().

    Greetings Rodolfo !!

    1. Ok!

  8. hello,
    thank you for the good job you are doing. Can the the js code show date rang like ‘ order today for delivery in 3 – 5 days, date.
    thanks.

    1. Hey Jay, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      1. Hi,
        I am able to figure it out. Thank you all the same for the good work.

        1. Cool!

  9. Hi

    I want to ask, what if I want to tell the customer the lead time like this: please allow 2-3 business days to processing

    I want to put it under the Add to Cart ”button

    tq

    1. Hi Rodolfo how can I implement this rule in a woocommerce in Spanish, the result that I choose as is in English
      thanks.

      1. Did you try changing the text to Spanish inside the snippet?

        1. yes i even replaced
          date_default_timezone_set (‘Europe / London’); by date_default_timezone_set (‘Europe / Madrid’) ;.
          It does not respond, it continues giving months and days in English, also “place your order before 4pm today and receive it today, it is not possible. Thank you for your help.

          1. Oh I see what you mean. You’re talking about date “format”, not Spanish words. Check https://www.php.net/manual/en/function.date.php

            1. Hi Rodolfo,

              First of all, thank you for your great work, information, sharing and caring!

              Again, great snippet! Wouldn’t it be great if you can have a in stock or on backorder dependency build-in?
              Any suggestions how?

              Thanks

              Okke

              1. It’d be great, yes – but unfortunately it’s custom work

                1. Dear Rodolfo,

                  I am a great fan of your site, i used a lot of your work to improve our website.

                  I was able to implement this snippet into our site it is working great, do you have a suggestion to make it compatible with wp-rocket cache?

                  The current problem with it that I have to clear the cache to make the snippet to update.

                  Take care!

                  Pal

                  1. Thanks Pal. If you have dynamic content, then you should exclude single product pages from cache

                    1. Dear Rodolfo. Thank you for your reply. In the meantime I found a workaround. Maybe somebody in the future learn from it. The snippet for me updates two times a day, i set up a cronjob to clear the cache of wp-rocket 1 minute after each update. This way I have cached product pages and the snippet always shows the right time.

                    2. Ah ok cool

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *