0
votes

I would like to know, in Wordpress + WooCommerce: How to remove a specific product or category from the cart count ?

I'm selling a product associated with a second product (which is a subscription) , and when I add 5 of this product to the Basket, the Mini Cart in the header shows 10 products.

This can be scary for the customers. I took a look at the similar questions, but it didn't work for the Cart in the header.

2

2 Answers

0
votes

You can filter the cart quantity via woocommerce_cart_contents_count. Change slug_of_category_to_ignore to suit.

/**
 * Filters the reported number of cart items.
 * Counts only items NOT in certain category.
 *
 * @param  int  $count
 * @return int
 */
function so_43498002_cart_contents_count( $count ) {

    $cart_items = WC()->cart->get_cart();
    $subtract   = 0;

    foreach ( $cart_items as $key => $value ) {

        if ( has_term( 'slug_of_category_to_ignore', 'product_cat', $value['product_id'] ) ) {
            $count -= $value[ 'quantity' ];
        }
    }

    return $count;
}
add_filter( 'woocommerce_cart_contents_count',  'so_43498002_cart_contents_count' );
0
votes

Try This

add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $total_products = WC()->cart->cart_contents_count;
    $multiples = 6;
    $totale = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $prodotti = $values['data'];

        if( ! has_term( array( 169, 152 ), 'product_cat', $prodotti->id ) ){
            $totale += $values['quantity'];
        } 

    }
    echo $totale;
    if ( ( $totale % $multiples ) > 0 ){
        wc_add_notice( sprintf( __('You need to buy in multiples of %d products', 'your-textdomain'), $multiples ), 'error' );
    }

}