首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Woocommerce:整个产品类别的最低数量,而不是变化

Woocommerce:整个产品类别的最低数量,而不是变化
EN

Stack Overflow用户
提问于 2015-01-20 03:09:49
回答 1查看 3.1K关注 0票数 2

因此,我已经安装了一个插件(“高级产品数量”),它允许我为类别和产品设置最小数量。然而,我要做的是为整个产品/类别设定一个最小的数量,而不是为每个单独的变化设置最小的数量。为了更好地理解这一点,我有一家烘焙食品公司,在那里我出售百吉饼。顾客至少需要订购12个百吉饼。他们可以订购他们想要的任何种类的百吉饼(例如:4个罂粟籽,5个普通面包,3个洋葱等),只要他们至少订购12个百吉饼。

当我为百吉饼类设定一个最小类别时,它会使购物车中的每种产品都增加到12种来完成结账,而不是认识到它们都是一种百吉饼,因此满足了整个百吉饼最少12个百吉饼的要求。

有没有人知道我怎样才能让这个最小数量的概念适用于我的情况?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-20 06:44:35

试试下面的代码:

代码语言:javascript
复制
    // Set minimum quantity per product before checking out
    add_action( 'woocommerce_check_cart_items', 'rohil_set_min_total' );
    function rohil_set_min_total() {
        // Only run in the Cart or Checkout pages
        if( is_cart() || is_checkout() ) {

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


                // Set minimum product cart total
                $minimum_cart_product_total = 12;

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

                    $total_quantity += $product['quantity'];

                endif;

            endforeach;

            if( $total_quantity < $minimum_cart_product_total ) {
                // Display our error message
                wc_add_notice( sprintf( '<strong>A Minimum of %s products is required from bagel category before checking out.</strong>'
                    . '<br />Current number of items in the cart: %s.',
                    $minimum_cart_product_total,
                    $total_quantity ),
                'error' );
            }

        }

    }

编辑:

代码语言:javascript
复制
    // Set minimum quantity per product before checking out
    add_action( 'woocommerce_check_cart_items', 'rohil_set_min_total' );
        function rohil_set_min_total() {
            // Only run in the Cart or Checkout pages
            if( is_cart() || is_checkout() ) {

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


                    // Set minimum product cart total
                    $minimum_cart_product_total = 12;

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

                        $total_quantity += $product['quantity'];

                    endif;

                endforeach;

                if ( has_term( 'bagels', 'product_cat', $product['product_id'] ) ) :
                    if( $total_quantity < $minimum_cart_product_total ) {
                        // Display our error message
                        wc_add_notice( sprintf( '<strong>A Minimum of %s products is required from bagel category before checking out.</strong>'
                            . '<br />Current number of items in the cart: %s.',
                            $minimum_cart_product_total,
                            $total_quantity ),
                        'error' );
                    }
                endif;

            }

        }

新编辑的

代码语言:javascript
复制
    // Set minimum quantity per product before checking out
    add_action( 'woocommerce_check_cart_items', 'rohil_set_min_total' );
        function rohil_set_min_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 minimum product cart total
                    $minimum_cart_product_total = 12;

                    // See if any product is from the bagel category or not
                    if ( has_term( 'bagels', '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( 'bagels', 'product_cat', $product['product_id'] ) ) :
                        if( $total_quantity < $minimum_cart_product_total && $i == 0 ) {
                            // Display our error message
                            wc_add_notice( sprintf( '<strong>A Minimum of %s products is required from bagel category before checking out.</strong>'
                                . '<br />Current number of items in the cart: %s.',
                                $minimum_cart_product_total,
                                $total_quantity ),
                            'error' );
                        }
                        $i++;
                    endif;
                endforeach;
            }

        }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28037139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档