has_term() all product categories but exclude some
-
Hi
I am trying to make two PHP variables. One must be assigned the value “true” if there is in the cart ONLY one or more products with the product categories which have the IDs ’21’, ’19’, ’16’, ’20’, ’22’.The second variable must be assigned the value “true” if there is one or more items in the cart with the product categories that have the IDs ’21’, ’19’, ’16’, ’20’, ’22’ AND one or more products from other product categories.
I’ve tried the code below, but it does not work… any suggestions?
// Loop through all products in the Cart foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $var1 = false; $var2 = false; $excluded_cats = array( '21', '19', '16', '20', '22' ); if ( has_term( array('21', '19', '16', '20', '22'), 'product_cat', $cart_item['product_id'] ) && has_term( $cart_item['product_id'], 'product_cat', $excluded_cats ) ) { $var1 = true; break; } else if (has_term( array('21', '19', '16', '20', '22'), 'product_cat', $cart_item['product_id'] ) && !has_term( $cart_item['product_id'], 'product_cat', $excluded_cats )) { $var2 = true; break; } }
What I need is just a variable that contains all product categories except the categories with the ids ’21’, ’19’, ’16’, ’20’, ’22’.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘has_term() all product categories but exclude some’ is closed to new replies.