I want to check the product in cart from which category is and if is from Category X to show some message.

So far I've done this and I've placed it in checkout.php

$categories = array('cat' => '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 ) {
   do_action( 'woocommerce_cart_collaterals' );
else {
    echo "Product from category X";

Sadly, here I see the else { echo "Product from category X"; } no matter if the product is from Category X or no.

var_dump($found); return NULL

New contributor
Ivanov is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Try using $categories = array( 39 ); ? – Sally CJ Dec 7 at 10:20
  • Still same, no matter what product is in the cart I see else block and $found is NULL – Ivanov Dec 7 at 10:22
  • 1
    Hm, after few refreshes and tests now it is working with $categories = array( 39 );. Thanks – Ivanov Dec 7 at 10:27

Your Answer

Ivanov is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.