$cart_item = $woocommerce->cart->get_cart();
I have the above code.
if I run print_r on cart_item I get a multi dimensional array:
Array( [a6292668b36ef412fa3c4102d1311a62] => Array ( [product_id] => 6803
How do I get the product_id only?
I tried $test = $cart_item['data'];
print_r($test);
Didn't work.
To get the
product ID
of each cart item in the foreach loop (for a simple product):If it's a variable product, to get the
variation ID
:Or for both cases (where
$cart_item['data']
is theWC_Product
Object in Woocommerce 3+):1) Breaking the loop (Just to get the first item ID (product ID) of cart):
You can use directly
$product_id
variable of the first item in cart.2) Using an array of product IDs (one for each item in cart).
$products_ids_array[0];
$products_ids_array[1];
etc…To check product categories or product tags in cart item use WordPress
has_term()
like: