Getting the ids of the items in your cart can be done somewhat like this:
global $woocommerce;
$cart = $woocommerce->cart->get_cart();
$cart_items_ids = array();
foreach ( $cart as $item_key => $item_value ) {
$cart_items_ids[] = $item_value[ 'data' ]->id;
}
OR Plese check below Snippet: Check if Product Category is in the Cart – WooCommerce
add_action('woocommerce_before_cart', 'xyz_check_category_in_cart');
function xyz_check_category_in_cart() {
// Set $cat_in_cart to false
$cat_in_cart = false;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// If Cart has category "download", set $cat_in_cart to true
if ( has_term( 'download', 'product_cat', $product->get_id() ) ) {
$cat_in_cart = true;
break;
}
}
// Do something if category "download" is in the Cart
if ( $cat_in_cart ) {
// For example, print a notice
wc_print_notice( 'Category Downloads is in the Cart!', 'notice' );
// Or maybe run your own function...
}
}
You can place PHP snippets at the bottom of your child theme functions.php file (before “?>” if you have it).
Please let me know in the comments if everything worked as expected.
Thank you!