wp query - Check the product in the cart from which category is and show message

admin2025-06-05  0

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749121236a316543.html

最新回复(0)