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.

1 Comments

  1. Woo User on October 8, 2021 at 11:27 am

    Hi There,

    In my site I’m using a custom product type which has been created with the plugin,
    i.e composite products, with this product type we are going to purchase many products within that, I have used this plugin for creating the composite product https://docs.yithemes.com/yith-composite-products-for-woocommerce/premium-version-settings/yith-woocommerce-product-add-ons/
    So, the main issue is whenever I add a product to cart the cart count considers all the products which are the child products inside that Main composite product, and I don’t want those child products to get counted, only main product should be counted.
    For ex: If my Composite Product has 5 Child items in that, when I add 1 composite product to cart then the cart count shows up as 6, which I dont want and to retain it 1 only.
    Any help towards this is highly appreciated
    Thanks

Leave all Comment