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

use*_*987 2 php wordpress woocommerce

因此,我已经安装了一个插件(“高级产品数量”),它允许我设置类别和产品的最小数量。然而,我想做的是为整个产品/类别设置最小数量,而不是为其每个单独变体设置最小数量。为了说明这一点,我有一家烘焙食品公司,卖百吉饼。顾客需要至少订购 12 个百吉饼。他们可以订购任意数量的各种百吉饼(例如:4 个罂粟籽、5 个原味、3 个洋葱等),只要他们总共订购至少 12 个百吉饼。

当我为百吉饼类别设置类别最小值时,它会使购物车中的每个产品变体将其数量增加到 12 个以完成结帐,而不是认识到它们都是百吉饼的一种类型,因此满足总体百吉饼的最低数量 12 个百吉饼。

有谁知道如何让这个最小数量概念适合我的情况?

Roh*_*ner 5

试试这个代码:

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

        }

    }
Run Code Online (Sandbox Code Playgroud)

编辑:

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

            }

        }
Run Code Online (Sandbox Code Playgroud)

新编辑

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

        }
Run Code Online (Sandbox Code Playgroud)