Cart & Checkout Message Based on Shipping Method & Class in WooCommerce

This code enables you to display a message based on the shipping method selected. The code :

  • Displays a custom message inline with the shipping method, based on the shipping method chosen
  • Displays the message if the product in the cart uses a specific shipping class
  • Displays the message on the cart and checkout pages.

The code is written based on this question from a member of the WooCommerce community :

  • I have two shipping methods set up: Free and Express.
  • I also have a Dangerous Goods (dangerous-goods) shipping class set up for some products that are not allowed to be shipping via Express.

QUESTION:

Does anyone have a snippet that will allow me to add a class to the Express shipping label at checkout, only if there is a ‘Dangerous Goods’ product in the cart?

ANSWER :

It’s rarely a 2 minute, click of the fingers, wave of the magic wand solution ( if it was, i would provide it as a free tutorial ). After several hours of modifying different versions of code using different methods, here’s the tested/working solution you can see in the following demo video :

Demo Video

Shows a custom message displaying underneath the shipping method on the cart and checkout pages when the product with shipping class slug dangerous-goods is added to the cart and the Express shipping method is chosen.

Usage

  1. Line 14, swap out your shipping class slug which you can find in the above screenshots.
  2. Line 23, swap out your shipping method ID which you can find using code like this which displays your ID’s on your shipping zones page wp-admin/admin.php?page=wc-settings&tab=shipping

Download Folder

Related Tutorials

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.