小编Gui*_*ume的帖子

根据类别名称woocommerce检查购物车中的产品?

如果某个类别的产品在我的购物车中,我试图触发一个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'产品,我想要一个很好的回复!

谢谢!

php cart categories woocommerce

5
推荐指数
1
解决办法
9701
查看次数

标签 统计

cart ×1

categories ×1

php ×1

woocommerce ×1