繁体   English   中英

如何限制woocommerce中每个类别的购物车中的物品

[英]How to limit item in the cart for each category in woocommerce

嗨,我有一个视觉图像,首先单击添加到购物车按钮,检查每个类别的数量配额的验证,在购物车小部件顶部输出错误消息。 在woocommerce中,如何计算购物车中每个类别的产品? 提前致谢

public function check_product_in_cart($product_in_cart) {
            //Check to see if user has product in cart
            global $woocommerce;

            // start of the loop that fetches the cart items

            foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];

                $terms = get_the_terms( $_product->id, 'product_cat' );

                // second level loop search, in case some items have several categories
                // this is where I started editing Guillaume's code

                $cat_ids = array();

                foreach ($terms as $term) {
                    $cat_ids[] = $term->term_id;
                }

                if(in_array(70, (array)$cat_ids) ||in_array(69, (array)$cat_ids) || in_array(68, (array)$cat_ids)) {
                    //if exist in "strain" category 

                    //output true

                   $product_in_cart = true;
                   //how to count all item in this category??? Thanks
                }

            }

            return $product_in_cart;

        }

在这里,我是如何计算购物车项目中类别的数量的。 首先查看购物车中是否有属于该类别的物品,如果找到,则获取该物品的数量并输出类别计数。 处理每个类别。

    public function check_product_in_cart($product_in_cart) {
        //Check to see if user has product in cart
        global $woocommerce;

        // start of the loop that fetches the cart items
        $aty = 0;
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];

            $terms = get_the_terms( $_product->id, 'product_cat' );

            // second level loop search, in case some items have several categories
            // this is where I started editing Guillaume's code

            $cat_ids = array();

            foreach ($terms as $term) {
                $cat_ids[] = $term->term_id;
            }

            if(in_array(70, (array)$cat_ids) ||in_array(69, (array)$cat_ids) || in_array(68, (array)$cat_ids)) {//popular

              //category is in cart!

               $product_in_cart = true;
               $product_catqty = $values['quantity'];//get the quantiy of the item
               $aty += $product_catqty;
            }
            $cat_qty =  $aty;
        }


        return $cat_qty;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM