Woocommerce:整体产品类别的最小数量,而不是变化

时间:2015-01-20 03:09:50

标签: php wordpress woocommerce

所以,我已经安装了一个插件(“高级产品数量”),允许我为类别和产品设置最小数量。但是,我要做的是为整个产品/类别设置最小数量,而不是为每个变体设置最小数量。为了说明这一点,我有一个烘焙食品业务,我出售百吉饼。客户需要订购至少12个百吉饼。他们可以订购他们想要的各种百吉饼(例如:4个罂粟籽,5个平原,3个洋葱等),只要他们至少订购12个百吉饼。

当我为百吉饼类别设置最小类别时,它会使购物车中的每个产品变化增加到12以实现结帐,而不是认识到它们都是百吉饼的类型,因此可以满足整个百吉饼的最低要求12个百吉饼。

有没有人对如何让这个最小数量概念适用于我的情况有任何想法?

1 个答案:

答案 0 :(得分:4)

试试这段代码:

    // 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' );
            }

        }

    }

<强>编辑:

    // 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;

            }

        }

新编辑

    // 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;
            }

        }