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

How to Display a Message Based on Specific Shipping Class in WooCommerce?

Do you want to display an additional message to customers based on a specific shipping class on the WooCommerce Checkout page? Then the code below helps to achieve this.

add_action( 'woocommerce_review_order_before_order_total', 'ts_checkout_shipping_class_message' );
function ts_checkout_shipping_class_message(){
    // Here your shipping class slugs in the array
    $shipping_classes = array('heavy-items');
    // Here your shipping message
    $message = __("Please add some shipping details in order notes field.", "woocommerce");

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        $shipping_class = $cart_item['data']->get_shipping_class();
        
        // echo '<pre>' . print_r($shipping_class, true) . '</pre>'; // Uncomment for testing

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

Output

Whenever a customer adds products belonging to a specific class (in this case, ‘heavy-items’), you can set a message as shown below to instruct them to do something.

How to Display a Message Based on Specific Shipping Class in WooCommerce?

You can also provide such custom messages early in the customer’s journey. Have a look into our post that guides well on how to show custom message based on shipping cass on single product page.

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

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x