Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为woocommerce中的每个类别限制购物车中的项目_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 如何为woocommerce中的每个类别限制购物车中的项目

Php 如何为woocommerce中的每个类别限制购物车中的项目,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,嗨,我有一个视觉图像, 首先单击“添加到购物车”按钮,检查每个类别的数量容差验证,在购物车小部件顶部输出错误消息。 如何使用woocommerce计算购物车中每个类别的产品? 提前谢谢 public function check_product_in_cart($product_in_cart) { //Check to see if user has product in cart global $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;
    }