Woocommerce: Minimum Quantity for Overall Product Category, not variations

So, I already have a plugin (“Advanced Product Quantities”) installed that allows me to set minimum quantities for categories and products. However, what I’m trying to do is set a minimum quantity for an overall product/category rather than a minimum for each of its individual variations. To put this into context, I have a baked goods business where I sell bagels. A customer needs to order a minimum of 12 bagels. They can order however many bagels of each kind that they want (for ex: 4 poppy seed, 5 plain, 3 onion, etc.), so long as they order a minimum of 12 bagels total.

When I set a category minimum for the bagel category, it makes each product variation in the shopping cart increase its quantity to 12 to fulfill checkout, rather than recognize that they are all a type of bagel and therefore fulfill the overall bagel minimum of 12 bagels.

Read More

Does anyone have any ideas on how I can get this minimum quantity concept working for my situation?

Related posts

Leave a Reply

1 comment

  1. Try out this code:

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

    EDITED:

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

    New EDITED

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