如果某个类别的产品在我的购物车中,我试图触发一个echo语句,这是我的代码:
<?php
//Check to see if user has product in cart
global $woocommerce;
//flag no book in cart
$item_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$_categoryid = $term->term_id;
}
if ( $_categoryid == 'name_of_category' ) {
//book is in cart!
$item_in_cart = true;
}
}
if ($item_in_cart === true) {echo 'YES';}
else {echo 'Nope!';}
?>
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?我的购物车中有'name_of_category'产品,我想要一个很好的回复!
谢谢!