I'm looking to use WooCommerce's add to cart validation to limit actions of a specific category.
I have two parent categories: Cat A and Cat B.
For Cat A, it should be unrestricted. So, it can be added to cart at any time.
For Cat B, I have different child categories. I'm looking to limit it so only one child category from Cat B can exist in the cart at any time. I'd like an error message to appear if someone tries to add a second Cat B child category product to cart when there's a conflicting child cat already in cart.
The child categories will be consistently changing, so querying by child cat ID isn't an option -- it has to be done through the parent. It's also an option to make all the Cat B child categories parent categories, but then I still need to exclude Cat A from the restrictions.
Based on Allow only one product per product category in cart answer code, this is what I have so far, trying to make it so the cart loop runs only if the product being added is not from Cat A:
add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {
// Getting the product categories slugs in an array for the current product
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat)
$product_cats[] = $obj_prod_cat->slug;
if ( ! $product_cats['cat-a-slug']) {
// Iterating through each cart item
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// When the product category of the current product does not match with a cart item
if( ! has_term( $product_cats, 'product_cat', $cart_item['product_id'] ))
{
// Don't add
$passed = false;
// Displaying a message
wc_add_notice( 'Only one product from a category is allowed in cart', 'error' );
// We stop the loop
break;
}
}
}
return $passed;
}
This does what I want for the Cat B product but also limits Cat A, which I don't want.
While there are similar questions with solid answers to this, I haven't found one that solves my issue. I can't seem to get it to either ignore Cat A so it's unrestricted, or to read the child categories properly.