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!
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 businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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;
}
Hello and thank you for sharing your code. Would it be possible to add an extra day if it falls on a public holiday?
Joelene, 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!
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?
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!
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!
Hi Jens! Did you try change the “Order by 4PM {$order_by} for delivery on {$del_day}” string?
How do I show the dates in Spanish?
You mean the date format? Check https://www.php.net/manual/en/function.date.php
how do i change the color of the box from green to possibly black or something?
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!
What about caching? A JS implementation would be cache-friendly
Not sure I understand
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?
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!
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 !!
Ok!
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.
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!
Hi,
I am able to figure it out. Thank you all the same for the good work.
Cool!
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
You can simply add text to the single product page: https://businessbloomer.com/woocommerce-add-text-add-cart-single-product-page/
Hi Rodolfo how can I implement this rule in a woocommerce in Spanish, the result that I choose as is in English
thanks.
Did you try changing the text to Spanish inside the snippet?
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.
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
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
It’d be great, yes – but unfortunately it’s custom work
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
Thanks Pal. If you have dynamic content, then you should exclude single product pages from cache
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.
Ah ok cool