In WooCommerce, if the cart contains 6 or more items from 2 specific categories, then I would like to set a specific tax class (tax-zero) only to these items (not the entire cart, so not change it for the other products).
I use this piece of code which calculates the number of items in cart that are from the 2 categories, but I can't find how to complete it in order to set them with my "tax-zero" tax class.
add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_taxes', 20, 1 );
function apply_conditionally_taxes( $cart ){
$item_count = $cart->get_cart_contents_count();
$kingcat_count = 0;
foreach ( $cart->get_cart() as $cart_item ) {
if ( has_term( 'patisseries', 'product_cat', $cart_item['product_id'] ) or has_term( 'viennoiseries-et-gaufres', 'product_cat', $cart_item['product_id'] ) ) {
$kingcat_count += $cart_item['quantity'];
//echo $kingcat_count;
}
}
}