WooCommerce cart total count minus certain product category

I have a client that sells boxes and units of a product – for the units they need to be sold in multiples of 6, for the boxes it doesn’t matter, so I need the cart total to be less the boxes total and checked to see if it is divisible by 6 – if not then display a warning.

The box products have a Product Category of ‘box’.

add_action('woocommerce_check_cart_items', 'custom_total_cart_items_count');
/**
 * WooCommerce - count cart items, remove some Prod Cat items, display error if remaining count is not divisble by a number
 * @link https://stackoverflow.com/questions/65769424/get-woocommerce-cart-items-count-except-for-a-product-category
 */
function custom_total_cart_items_count() {
    // Below your category term ids, slugs or names to be excluded
    $excluded_terms = array('box'); 

    $items_count    = 0; // Initializing

    // Loop through cart items 
    foreach ( WC()->cart->get_cart() as $item ) {
        // Excluding some product category from the count
        if ( ! has_term( $excluded_terms, 'product_cat', $item['product_id'] ) ) {
            $items_count += $item['quantity'];
        }
    }
   // echo $items_count;
    $multiple_of = 6; // <= Here set the "multiple of" number

    if ( $items_count % $multiple_of != 0 ) {
        wc_add_notice( sprintf( __('You need to buy single bottles in quantities of %s, same type or mix and match', 'woocommerce'), $multiple_of ), 'error' );
    }
}

So the Product Category ‘box’ is set as a variable in an array, a count is initialised and the cart loop of items is run, if the item is not in the excluded array the item count is incremented until the loop ends.

Finally the item count total is compared the multiple amount (6) with the modulo operator and a warning is displayed if the amount is not divisible.

If you find that you may only need to find an equal divisible amount just on 1 Product Category you can inverse the code.

add_action('woocommerce_check_cart_items', 'custom_total_cart_items_count');
/**
 * WooCommerce - count cart items, remove some Prod Cat items, display error if remaining count is not divisble by a number
 * @link https://stackoverflow.com/questions/65769424/get-woocommerce-cart-items-count-except-for-a-product-category
 */
function custom_total_cart_items_count() {
    // Below your category term ids, slugs or names to be excluded
    $included_terms = array('bottle'); 

    $items_count    = 0; // Initializing

    // Loop through cart items 
    foreach ( WC()->cart->get_cart() as $item ) {
        // Including some product category from the count
        if ( has_term( $included_terms, 'product_cat', $item['product_id'] ) ) {
            $items_count += $item['quantity'];
        }
    }
   // echo $items_count;
    $multiple_of = 6; // <= Here set the "multiple of" number

    if ( $items_count % $multiple_of != 0 ) {
        wc_add_notice( sprintf( __('You need to buy single bottles in quantities of %s, same type or mix and match', 'woocommerce'), $multiple_of ), 'error' );
    }
}

So above the only changes are that variable name is changed to $included_terms and I have a different Product Category of ‘bottle’. This time in the loop item count only products in that category will be incremented in the loop – then finally the same modulo comparison is made.

Leave all Comment