Search code examples
phpwordpresswoocommercecartnotice

Cart Message for a Specific Shipping Class in WooCommerce Based on Number of Cart Items


This is adapted from Cart Message for a Specific Shipping Class in WooCommerce answer and Show or hide shipping methods based on number of cart items for specific shipping class answer.

This is my code attempt:


add_action( 'woocommerce_before_calculate_totals', 'cart_items_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $shipping_class_id = '150'; // Your shipping class Id
    
    $allowed_max_qty     = 4; // Max allowed quantity for the shipping class
      
    $related_total_qty   = 0; // Initializing

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        if( ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) ){
            $related_total_qty += $cart_item['quantity'];
        }
    }
    
    // When total allowed quantity is more than allowed (for items from defined shipping classes)
    if ( $related_total_qty > $allowed_max_qty ) {
            wc_clear_notices();
            wc_add_notice( sprintf('Beers can only be shipped via pallet shipping. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.', 'woocommerce'),
                'notice' );
            break;
        }
    }
}


But it doesn't seem to be working. Any ideas?

UPDATE: I got it working. Here's the code:


add_action( 'woocommerce_before_calculate_totals', 'cart_items_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $shipping_class_id = '150'; // Your shipping class Id
    
    $allowed_max_qty     = 4; // Max allowed quantity for the shipping class
      
    $related_total_qty   = 0; // Initializing

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        if( ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) ){
            $related_total_qty += $cart_item['quantity'];
        }
    }
    
    // When total allowed quantity is more than allowed (for items from defined shipping classes)
    if ( $related_total_qty > $allowed_max_qty ) {
            wc_clear_notices();
            wc_add_notice( sprintf('Beers can only be shipped via pallet shipping. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.', 'woocommerce'),
                'notice' );
        }
}



Solution

  • Works just fine.

    
    add_action( 'woocommerce_before_calculate_totals', 'cart_items_shipping_class_message', 20, 1 );
    function cart_items_shipping_class_message( $cart ){
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        $shipping_class_id = '150'; // Your shipping class Id
        
        $allowed_max_qty     = 4; // Max allowed quantity for the shipping class
          
        $related_total_qty   = 0; // Initializing
    
        // Loop through cart items
        foreach( $cart->get_cart() as $cart_item ) {
            if( ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) ){
                $related_total_qty += $cart_item['quantity'];
            }
        }
        
        // When total allowed quantity is more than allowed (for items from defined shipping classes)
        if ( $related_total_qty > $allowed_max_qty ) {
                wc_clear_notices();
                wc_add_notice( sprintf('Beers can only be shipped via pallet shipping. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.', 'woocommerce'),
                    'notice' );
            }
    }