Woocommerce过滤车和类别具体数量

时间:2015-02-27 07:36:39

标签: php wordpress filter woocommerce cart

基本上,我正在尝试过滤我的购物车。 如果“cuvees”类别的产品在购物车中的数量为4,5,7,8,9,10,11,13,14,15,16,17,19,21,我想显示以下信息。

到目前为止,我已经做了但它只适用于一个值:7。 我声明函数时是否需要放一个数组?

add_action( 'woocommerce_check_cart_items', 'check_total' );
    function check_total() {
        // Only run in the Cart or Checkout pages
        if( is_cart() || is_checkout() ) {

            global $woocommerce, $product;
            $i=0;
            //$prod_id_array = array();
            //loop through all cart products
            foreach ( $woocommerce->cart->cart_contents as $product ) :


                // Set checking if there's y products in cuvees cart total
                $cart_product_total = 4;



                // See if any product is from the cuvees category or not
                if ( has_term( 'cuvees', 'product_cat', $product['product_id'] ) ) :

                    $total_quantity += $product['quantity'];
                    //array_push($prod_id_array, $product['product_id']);
                endif;

            endforeach;

            foreach ( $woocommerce->cart->cart_contents as $product ) :
                if ( has_term( 'cuvees', 'product_cat', $product['product_id'] ) ) :
                    if( $total_quantity == $cart_product_total && $i == 0 ) {
                        // Display our error message
                        wc_add_notice( sprintf( '<h5 style="letter-spacing:0.5px;color:white;text-align:center;">/!\&nbsp;    Une commande de %s bouteilles n&#39;est pas possible&nbsp;! &nbsp;   /!\ </h5><br /> <br /><p style="text-align:center;"> L&#39;envoi n&#39;est possible que pour 1 | 2 | 3 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60  | 72 | 96 | 120 et plus bouteilles.</p>',
                            $cart_product_total,
                            $total_quantity ),
                        'error' );
                    }
                    $i++;
                endif;
            endforeach;
        }
    }        

谢谢你们!

1 个答案:

答案 0 :(得分:2)

我将在黑暗中采取刺戳(我仍然不是100%确定要求,但似乎你需要一个阵列)。请尝试以下代码:

add_action( 'woocommerce_check_cart_items', 'check_total' );
    function check_total() {
        // Only run in the Cart or Checkout pages
        if( is_cart() || is_checkout() ) {

            global $woocommerce, $product;
            $i=0;
            //$prod_id_array = array();
            //loop through all cart products
            foreach ( $woocommerce->cart->cart_contents as $product ) :


                // Set checking if there's y products in cuvees cart total
                $cart_product_total = array(4,5,7,8,9,10,11,13,14,15,16,17,19,21);



                // See if any product is from the cuvees category or not
                if ( has_term( 'cuvees', 'product_cat', $product['product_id'] ) ) :

                    $total_quantity += $product['quantity'];
                    //array_push($prod_id_array, $product['product_id']);
                endif;

            endforeach;

            foreach ( $woocommerce->cart->cart_contents as $product ) :
                if ( has_term( 'cuvees', 'product_cat', $product['product_id'] ) ) :
                    if( in_array($total_quantity, $cart_product_total) && $i == 0 ) {
                        // Display our error message
                        wc_add_notice( sprintf( '<h5 style="letter-spacing:0.5px;color:white;text-align:center;">/!\&nbsp;    Une commande de %s bouteilles n&#39;est pas possible&nbsp;! &nbsp;   /!\ </h5><br /> <br /><p style="text-align:center;"> L&#39;envoi n&#39;est possible que pour 1 | 2 | 3 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60  | 72 | 96 | 120 et plus bouteilles.</p>',
                            $total_quantity ),
                        'error' );
                    }
                    $i++;
                endif;
            endforeach;
        }
    }

如果仍然没有回答您的问题,请澄清“4,5,7,8,9,10,11,13,14,15,16,17,19,21的数量是多少”推车。“在原始问题中意味着,我可以更好地帮助你。