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

How to show Cart Message for a Specific Shipping Class & Minimal Cart Total in WooCommerce?

To encourage customers to order more and in return deliver their products at the doorstep, you can display a friendly message in the shopping cart. The provided code helps to achieve this by adding a notice in the cart for specific shipping class and a minimal cart total in WooCommerce.

add_action( 'woocommerce_before_calculate_totals', 'ts_cart_items_shipping_class_message', 20, 1 );
function ts_cart_items_shipping_class_message( $cart ){
    if ( is_admin() && ! defined('DOING_AJAX') )
        return;

    ## Your settings below ##
    $shipping_class = 'heavy-items'; // The targeted shipping class
    $min_amout      = 75; // The minimal amount

    $amout_incl_tax = 0; // Initializing variable

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        // Targeting our defined shipping class only
        if( $cart_item['data']->get_shipping_class() === $shipping_class ){
            // Add each subtotal from our defined shipping class only
            $amout_incl_tax += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
        }
    }

    // Display an error notice when quantity count is below the minimum defined on cart page only
    if( $amout_incl_tax > 0 && $amout_incl_tax < $min_amout && is_cart() ){
        wc_clear_notices(); // Clear all other notices
        // Display our custom notice
        wc_add_notice( sprintf( 'Order <strong>%s</strong> more to deliver your order at home with our delivery service.',
            wc_price($min_amout - $amout_incl_tax) ), 'notice' );
    }
}

If the cart total is below $100 and contains items with the “Big Package” shipping class, a notice will be displayed in the cart, prompting customers to add more items to qualify to deliver the product at their doorstep.

How to show Cart Message for a Specific Shipping Class and a Minimal Cart Total in WooCommerce?

Similarly, you can also show the cart notice message to display the remaining cost for customer to avail free shipping on your WooCommerce store that clearly indicates customers on how much should they purchase to get free shipping.

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

Share It:

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