I would like to check item gonna be add to cart is already in cart and belong to which category, if that category have over 5 products then throw an exception message:
function check_product_in_cart() {
//Check to see if user has product in cart
global $woocommerce;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
// second level loop search, in case some items have several categories
$cat_ids = array();
foreach ($terms as $term) {
$cat_ids[] = $term->term_id;
}
if(in_array(434, (array)$cat_ids) || in_array(435, (array)$cat_ids)) {
//category is in cart!
$product_in_cart = true;
//count the category?
$count = 1;
while($product_in_cart === true) {
$count +=1;
}
}
}
return $product_in_cart;
return $count;
}
then echo out the error message can not add to cart. what is the best way to count product in cart per category?. Basically, I want to limit item add to cart per category. Thanks guys