What I'm trying to do is to take the categories for products which are in the cart. Then check via some conditions what category id is and show different notice.
So far I have this
/**
* Cart collaterals hook.
*
* @hooked woocommerce_cross_sell_display
* @hooked woocommerce_cart_totals - 10
*/
$categories = array( 39 );
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$found = true; // Set to true
break; // Stop the loop
}
}
if( $found ) {
echo 'Category with ID = 39';
}
else if ($found != $cart_item['category_ids']) {
echo "Category with ID = 39 and other ID('s)";
}
else {
"Category with ID != 39";
}
Seems like $cart_item['category_ids']
doesn't return categories. $found
is working and showing category with ID = 39.
When I var_dump($cart_items)
I do see different categories like this:
["category_ids"]=>
array(1) {
[0]=>
int(39)
}
/// another stuff in the array
["category_ids"]=>
array(2) {
[0]=>
int(32)
[1]=>
int(33)
}
So, in cart I have products from categories with id 39
, 32
and 33
.
I've tried also WC()->cart->get_cart()->category_ids
but this return NULL
UPDATE: This
$cat_ids = array();
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cat_ids = array_merge(
$cat_ids, $cart_item['data']->get_category_ids()
);
}
$cat_id = 39;
if ( in_array( $cat_id, $cat_ids ) ) {
echo 'Only 39 ';
} elseif ( ! empty( $cat_ids ) ) {
echo '39 + other';
} else {
echo ' all other without 39';
}
Currently matching
When: category 39 + other -> Only 39
When: all other without 39 -> 39 + other
When: Only 39 -> Only 39
It should be
When: category 39 + other -> 39 + other
When: all other without 39 -> all other without 39
When: Only 39 -> Only 39
UPDATE
When: Category 39 + product from other category ( Category ID = 32, 33 etc )
var_dump(count( $cat_ids )); -> int(1)
var_dump($has_cat); -> bool(true)
var_dump(cat_ids); -> array(1) { [0]=> int(39) } <---- there are 3 products in the cart 2 of them are from other categories.
When: Category 39 only
var_dump(count( $cat_ids )); -> int(1)
var_dump($has_cat); -> bool(true)
var_dump(cat_ids); -> array(1) { [0]=> int(39) }
When: No category 39
var_dump(count( $cat_ids )); -> int(2)
var_dump($has_cat); -> bool(false)
var_dump(cat_ids); -> array(2) { [0]=> int(32) [1]=> int(33) } <--- product is added in 2 categories
UPDATE 2
Condition 1
1) cat 30;
2) cat 39;
$cond = 2 (because there are products in cart from 39 + other category)
Condition 2
1) cat 39;
$cond = 1 (because in cart is/are product/s only from cat 39)
Condition 3
1) cat 40;
2) cat 15;
$cond = last one (because there is no product/s from cat 39 in cart)