Please forgive me in advance if this is a stupid question.
So, I'm currently doing a check on all the items in the WooCommerce Cart.
If there's a product in the cart is in the category 'Practice' and there are no products in the cart with the category 'Qualifying', I want to show Message 1
If both 'Practice' and 'Qualifying' categories are in the cart, I want to show message 2.
Right now, here's my code:
<?php
$category_checks = array();
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$practice = false;
$qualifying = false;
if( has_term( 'practice', 'product_cat', $product->id ) && ! has_term( 'qualifying', 'product_cat', $product->id ) ) : ?>
<?php $practice = true; ?>
Upsell goes here (Message 1)
<?php elseif( has_term( 'practice', 'product_cat', $product->id ) && has_term( 'qualifying', 'product_cat', $product->id ) ) : ?>
<?php $qualifying = true; ?>
No need to upsell (Message 2)
<?php endif; ?>
<?php
array_push( $category_checks, $practice, $qualifying );
}
if ( ! in_array( false, $category_checks, true ) ) {
}
?>
I have two products that have one category each, one has 'Practice' category and one has 'Qualifying' category.
When I add these two products to the cart and try this code, I constantly get Message 1, even though I'm saying if it doesn't (!) have the term 'qualifying'.
What am I doing wrong here?