Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Display a Message Based on Shipping Class in WooCommerce @Checkout Page?

As an online store owner, you would require notices to let your customers know about special handling instructions for certain shipping class items during the checkout process.  The handy code snippets can display a little note to your customers when someone is about to finalize their order, ensuring they’re aware of anything special related to the items they’re buying. 

In this guide, we’ll show how to display customized messages based on the type of shipping class a product belongs to during the WooCommerce checkout. 

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Preliminary Steps

Make a note of the shipping class ID for which you want to set the message.

How to find the Shipping class ID

  • Go to “Products” in the dashboard.
  • Hover over a product and click on edit.
  • Under the Product Data section select “Shipping “.
  • Right-click on the shipping class, select “Inspect,” and find the Shipping Class ID in the HTML.

How to Display a Message Based on Shipping Class in WooCommerce @Checkout Page? - Tyche Softwares

Solution: How to Display a Message Based on a Specific Shipping Class in WooCommerce Checkout

Let’s say you run an electronic store, and you want to show a message to customers when they’re buying a large appliance, like a TV. With the below given code snippet, you can easily display a message during checkout, letting them know that their order includes a heavy electronic item requiring extra care during shipping.

add_action('woocommerce_review_order_before_order_total', 'ts_checkout_shipping_class_message');

function ts_checkout_shipping_class_message() {
    // Here your shipping class IDs in the array
    $shipping_class_ids = array(22); // Replace 22 with your actual shipping class ID(s)

    // Here your shipping message
    $message = __("This package contains a heavy electronic item, like an LED television. Please note that special handling is required during shipping.", "woocommerce");

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $cart_item) {
        $shipping_class_id = $cart_item['data']->get_shipping_class_id();

        // Uncomment the line below for testing
        // echo '<pre>' . print_r($shipping_class_id, true) . '</pre>';

        // Check cart items for specific shipping class, displaying a message
        if (in_array($shipping_class_id, $shipping_class_ids)) {
            echo '<tr class="shipping-note">
                <td colspan="2"><strong>' . __("Note", "woocommerce") . ':</strong> ' . $message . '</td>
            </tr>';
            break;
        }
    }
}

Output

The output displays a specific message if a heavy electronic item, identified by a specific shipping class, is present in the cart.

How to Display a Message Based on Shipping Class in WooCommerce @Checkout Page

Code Explanation

Hook Registration:

  • The code begins by registering an action hook woocommerce_review_order_before_order_total. This hook triggers the execution of the function ts_checkout_shipping_class_message just before the order total is displayed during the checkout process.

Function Definition:

  • The function ts_checkout_shipping_class_message is defined to handle the logic for displaying a message based on a specific shipping class.

Shipping Class IDs Array:

  • An array named $shipping_class_ids is created, containing the shipping class ID(s) that the code will check against. In this example, it includes the ID 22.

Message Definition:

  • The variable $message holds a descriptive message indicating that the package contains a heavy electronic item, like an LED television, and special handling is required during shipping.

Loop through Cart Items:

  • The code initiates a loop to iterate through each item in the WooCommerce cart using WC()->cart->get_cart().

Fetch Shipping Class ID:

  • For each cart item, the shipping class ID is retrieved using $cart_item[‘data’]->get_shipping_class_id().

Testing Output (Optional):

  • There’s an optional line for testing purposes, allowing developers to uncomment and display the shipping class ID for each cart item.

Check for Specific Shipping Class:

  • The code checks if the current cart item’s shipping class ID is present in the array of specified shipping class IDs using in_array().

Display Message:

  • If a matching shipping class is found, a custom message is displayed in the WooCommerce checkout page within a table row (<tr>). The message is wrapped in a table cell (<td>) and styled as a shipping note.

Exit Loop:

  • The break statement is used to exit the loop after the message is displayed for the first matching item. This ensures that the message is not repeated for multiple items with the same shipping class.

Conclusion

This post guides you to display a notice based on the shipping class giving customers a message about unique shipping requirements for specific items. Irrespective of the specific shipping class items, you can also add the shipping & delivery notices on the WooCommerce checkout page.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
2 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Joy
1 month ago

Why this code is not working for me? my shipping class id is 1659.

Saranya
1 month ago
Reply to  Joy

Hi Joy,

The code has been tested and it works well in the updated WooCommerce version (8.5.1) of the classic checkout page. Please try switching to a default theme and deactivate all plugins except WooCommerce to check for any plugin conflicts. 
If you’re checking it within the checkout blocks page, further customizations are needed, as outlined in this documentation.

2
0
Would love your thoughts, please comment.x
()
x