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

How to Highlight Products in WooCommerce Cart Based on Shipping Class?

Shipping heavy or bulky items often comes with challenges, especially when it comes to delivery restrictions. WooCommerce’s shipping classes allow store owners to manage these situations effectively by grouping products and imposing shipping rules. For instance, you may only want to allow local pickup for certain items due to their weight or size. In such cases, it’s important to highlight restricted products in the cart and inform customers of delivery limitations. Lets see how to highlight WooCommerce cart items of specific shipping class with a message notifying customers about the undeliverable products.

Solution: Highlight Products in WooCommerce Cart Based on Shipping Class

The code snippet will help to easily highlight products in cart that have shipping restrictions as defined in the code. Specifically, if products belonging to the ‘heavy-items’ shipping class are present in the cart, they will be visually highlighted with a light red background. Additionally, a notice message gets displayed that such highlighted products will not be shipped.

// Add a custom class to cart items that need highlighting
add_filter('woocommerce_cart_item_class', 'ts_add_custom_class_to_cart_item_by_shipping_class', 10, 3);
function ts_add_custom_class_to_cart_item_by_shipping_class($class, $cart_item, $cart_item_key) {
    // Get the product object
    $product = $cart_item['data'];
    
    // Get the shipping class term slug
    $shipping_class_slug = get_term_by('id', $product->get_shipping_class_id(), 'product_shipping_class')->slug;
    
    // Define the specific shipping class slug
    $restricted_shipping_class_slug = 'heavy-items';
    
    // Check if the shipping class matches 'heavy-items'
    if ($shipping_class_slug === $restricted_shipping_class_slug) {
        $class .= ' custom-highlight'; // Add a custom class
    }
    
    return $class;
}

// Add custom CSS for the cart items with the custom-highlight class
add_action('wp_head', 'ts_add_custom_css_for_cart_highlight');
function ts_add_custom_css_for_cart_highlight() {
    if (is_cart()) {
        echo '<style>
            .woocommerce-cart-form table tbody tr.custom-highlight td {
                background-color: #ffcccc; /* Light red color */
            }
            .cart-error-message {
                margin: 20px 0;
                padding: 10px;
                background-color: #ffe6e6; /* Light red background */
                color: #d8000c; /* Dark red text */
                border: 1px solid #d8000c;
                border-radius: 5px;
                font-size: 16px;
            }
        </style>';
    }
}

// Display a combined notice message if there are restricted products
add_action('woocommerce_before_cart', 'ts_display_combined_error_message');
function ts_display_combined_error_message() {
    // Define the specific shipping class slug
    $restricted_shipping_class_slug = 'heavy-items';
    
    // Initialize array to store product names
    $restricted_products = array();
    
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        // Get the product object
        $product = $cart_item['data'];
        
        // Get the shipping class term slug
        $shipping_class_slug = get_term_by('id', $product->get_shipping_class_id(), 'product_shipping_class')->slug;
        
        // Check if the product belongs to the restricted shipping class
        if ($shipping_class_slug === $restricted_shipping_class_slug) {
            // Add product name to the array for the notice
            $restricted_products[] = get_the_title($product->get_id());
        }
    }
    
    // Display a combined notice message if there are restricted products
    if (!empty($restricted_products)) {
        $products_list = implode(', ', $restricted_products);
        echo '<div class="cart-error-message">';
        echo sprintf(
            'The following products cannot be shipped due to shipping restrictions: %s.',
            $products_list
        );
        echo '</div>';
    }
}
flexi bogo cta banner image


This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…

BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?

Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.

Output

First, to test the output, ensure that you have set up product-specific shipping classes and assigned some products to a shipping class. Let’s take a look at the items assigned to the ‘Heavy Items’ shipping class.

set up product-specific shipping classes

Here, we have filtered the products using the shipping class slug ‘heavy-items’. You can check this post which explains how to filter WooCommerce products by Shipping Class on WooCommerce Products page.

When a customer adds products to the cart, if any of them is assigned to the ‘heavy items’ shipping class, those items will be highlighted with a different color. Additionally, a notice will be displayed on the cart page indicating that these highlighted products cannot be shipped.

We’ve explored how to highlight undeliverable items and notify customers in advance about shipping restrictions. On the flip side, if you’re looking to attract more customers based on shipping class, consider tying a BOGO offer to specific shipping classes in WooCommerce. This approach can help boost your regular sales while offering targeted promotions.

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