In e-commerce, you must convey proper shipping information to the customers. This is how your customers stay informed all the time. Thereby, it avoids shipping related issues. One can provide such info below single product pages. You can manually update the shipping details below the ‘add to cart’ button. However, why do it manually when you can code with PHP? So, let’s identify coding about WooCommerce display shipping date at product page.
Such pre-coded data also has many other benefits. If in case you modify dispatch rules in future, then you can just change PHP coding. In short, you are not required to manually update single product pages. It is convenient & time-saving.
So, we will learn how to fix cut-off times (hour of the day) & current day of the week. This is how you can show up accurate shipping dates with time elements.
Here is an image showcasing estimate delivery date with time element:
PHP Snippet: WooCommerce Display Shipping Date at Product Page
Case scenario:
- Friday/Saturday/Sunday orders will be shipped on Monday
- For every other day, if ordered before 4 pm, shipps today
- If ordered after 4 pm, shipping will be made tomorrow
Take a look over “date(‘N’)” and “date(‘H’)” functions. These functions denote the current day of the week & current hour of the day. You can compare these functions with local & current time. Also, one can set different time zones by looking over the “date_default_timezone_set()” function.
add_action( 'woocommerce_after_add_to_cart_form', 'phpsof_dispatch_info_single_product' );
function phpsof_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 do you add this snippet?
So, this is how you activate this snippet named WooCommerce display shipping date at product page. This in turn will inform the estimated delivery date to your customer. Also, it is easier to activate this function with this code. So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file.
Is this snippet still valid?
So, this way, you can learn about WooCommerce display shipping date at product page. I have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected. Share it further if you find this snippet useful for you and it had saved your time.
Also Read, How to enable WooCommerce Switch Image Background on color change?
How to Sync Product Quantities at Cart in WooCommerce?
Important link: WooCommerce – https://woocommerce.com/