I have a minimum order amount of 3 products from Chilled category.
If there are no Chilled category products in the cart and any combination of other product categories (Bundle, Ambient, Gifts) in the cart, the customer should be able to checkout.
If there is less than 3 Chilled category products in the cart but there is also a Bundle category product in the cart, the customer should still be able to checkout.
My wc_add_notice error is counting the Chilled logic correctly, but it is displaying even when there are no items from the Chilled category in the cart.
Please can someone help where I am going wrong?
// Set minimum quantity per product before checking out
add_action( 'woocommerce_check_cart_items', 'chilled_set_min_total' );
function chilled_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce, $product;
$i=0;
// Set minimum product cart total
//And initialise product catergory counters
$minimum_chilled = 3;
$total_chilled = 0;
$cat_in_cart = false;
//check if chilled product in cart. If exists add to quantity
foreach ( $woocommerce->cart->cart_contents as $product ) :
if ( has_term( 'chilled', 'product_cat', $product['product_id'] ) ) {
$total_chilled += $product['quantity'];
}
if ( has_term( 'bundles', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
endforeach;
//if chilled items is less than minimum display error
if ($total_chilled < $minimum_chilled) {
wc_add_notice( sprintf( '<strong>A Minimum of %s products are required from The CHILLED category before checking out.</strong>'
. '<br />Current number of Chilled items in the cart: %s.',
$minimum_chilled,
$total_chilled ),
'error' );
}
}
}