Custom Shipping Date Notice on WooCommerce Checkout Page

Like I’ve written in previous articles, I learn so much by reading real-world requests from WooCommerce shop owners in Facebook groups and then researching and implementing a working solution. I recently saw a question where a shop owner wanted a custom shipping date notice displayed on the Checkout page, 3 days out from the current date. Continue reading and see how I solved this. Suggestions welcomed.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.


Note: All custom PHP code is placed in the child themeโ€™s (the default Storefront theme with minimal customization in this case) functions.php file in a development/testing environment.


Of course, there are probably several plugins that can achieve this same requirement. But, why install unnecessary plugins when we don’t need them?

To begin with, here are some things as a developer I think we should be mindful of when coding this solution:

  • Verify that any items in the cart are not virtual products (e.g., ebooks, subscriptions, coaching calls, etc) – It doesn’t make sense to display a shipping date notice for products that are not shipped anyway.
  • Optional – Adjust the 3-day window depending if the current date falls on the weekend. Let’s assume hypothetically that this shop runs a Monday through Friday operation. (This will be a future article).

We are going to target the area just below the Place order button on the Checkout page as shown in the red square in the image below:

Like with pretty much everything in WordPress, we can target specific hooks to place our own code on the page(s). For this example, I am hooking into the woocommerce_review_order_after_submit hook using the add_action() function and my own callback function:

add_action( 'woocommerce_review_order_after_submit', 'custom_woo_checkout_shipping_date_notice' , 9999, 3 );

function custom_woo_checkout_shipping_date_notice() {
date_default_timezone_set( 'America/Chicago' );
$is_virtual_product = false;
$current_date = date('F j, Y');
//get all the items in the cart so we can see if there are any virtual items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item_value ) {
if ( $cart_item_value['data']->is_virtual() ) $is_virtual_product = true; //there is a virtual product in the cart
}
if ( $is_virtual_product ) {
return;
}
_e('Your order should ship by, '.'<b>'.date('F jS, Y', strtotime($date. ' + 3 days')).'</b>'. ' if you place your order today.', 'woocommerce');
}

The key takeaways from this code are:

  • Get all the products in the current Cart
  • Verify if any products in the current Cart are virtual products.
  • If any shopping Cart products are virtual, then just return from the callback function, essentially doing nothing.
  • If any of the products are physical products that will ship, display the estimated shipping date by adding 3 days to whatever the current date is and show the message below the Place order button.

If I add the physical ‘Beanie’ product to the Cart and proceed to the Checkout page, we can see the custom shipping date notice displayed below the Place order button. Since the day of this articles’ draft is March 14th, 2023, we have a 3-day shipping ‘out date’ displayed for March 17th, 2023:

Credit Resources

Like always, I owe my success and ability to figure out working solutions due to fantastic content freely available online. These 2 articles in particular were instrumental in my understanding and coming up with the code snippet solution:


If you see any mistakes in the code or know of a better solution, please share them freely in the comments section.

Thank you for reading this post. Please share it with someone else who would enjoy it as well.


Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.

Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.

More ways I can help

Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.


๐Ÿ“ฐ Get your brand, product, or service the attention it deserves with affordable classified ad placement in the OpenLampTech newsletter. I appreciate your support!


One thought on “Custom Shipping Date Notice on WooCommerce Checkout Page

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.