We hadn’t published any Ask Sell with WP questions for some time, so we’re catching up on a copy from last week and this week.

Today’s question is from Duriez:

My WooCommerce site sells 5 types of wine but we make shipping by a box of 6 bottles. Is there a way way get customers order any wine but they have to order in groups of 6? So in checkout, we have to be able to have order multiples of 6 bottles with the 5 different products. Minimum order of 6. No maximum.


Selling WooCommerce product groups definitely doable, and there are a few ways to approach this problem depending on how your minimum quantity should be enforced.

The WooCommerce Min/Max Quantities plugin ($29) will help out with a couple potential set ups, and it’s a nifty little plugin for setting order minimums, maximums, and product groupings (i.e., order in quantities of 6).

WooCommerce Product Groups: Order Minimum Quantity

Min/Max Quantities can easily set order quantity minimums. If wine is the only product in the shop being sold in this scenario, then limiting the order to minimums of 6 bottoms will be very straightforward.

The plugin documentation is very helpful for this and has tons of screenshots. In order to set up an order minimum, you can set the “Minimum order quantity” field under WooCommerce > Settings > Products > General:

WooCommerce Order Minimum Quantity

This will restrict any order to require a minimum of 6 total items in the cart.

With that said, what if other products aside from wine are being sold in the store? We’ll need a category restriction instead, which our next two scenarios will cover.

WooCommerce Product Groups: Require Category Groups

Min/Max Quantities can also sell your products in category groups. If you only want to sell the wine in multiples of 6 bottles, then you can create a category group by editing the product category and setting the “Group of…” field to 6.

WooCommerce category group required

This will let customers choose from any product in the “Wine” category, and will allow purchases of 6, 12, 18, etc bottles at one time, requiring both a category minimum and groups of 6 to order.

WooCommerce Product Groups: Require Category Minimum

Now the final scenario: what if you want to require only a category minimum? For example, you want to require 6 products from the “wine” category, but allow any quantity above this amount?

This is where it gets tricky to do, as I’m not aware of any plugin that handles this use-case (Min/Max Quantities only does category groups, not minimums).

This seemed like an interesting problem to tackle, so I wrote up a tiny custom code snippet to do just this. You should only use this snippet if you know how to add custom code to your site, as it worked for me in my test store but I’m not taking responsibility for your site 🙂

This snippet consists of two functions: one that gets the total quantity of items in the cart from a particular category, and one function to compare that amount our category minimum, then render a notice and block checkout if it doesn’t meet the minimum.

For those of you interested in how WooCommerce restricts or allows checkout, there’s a really easy way to block checkout: render a notice with the “error” type in the cart check, as WooCommerce will throw its own Exception at checkout to stop the checkout from occurring if an error notice is present.

I’ll use this to my advantage to render a notice if my category quantity is below my minimum quantity.

First, the function to get the category quantity in the cart: we’ll get the quantities of the line items, check the category for each line item, and if it belongs to our desired category, we’ll add it to the running “category total”.

function sww_get_category_quantity_in_cart( $category_id ) {

    // get the quantities of cart items to check against
    $quantities = WC()->cart->get_cart_item_quantities();
    
    // start a counter for the quantity of items from this category
    $category_quantity = 0;

    // loop through cart items to check the product categories
    foreach ( $quantities as $product_id => $quantity ) {

        $product_categories = get_the_terms( $product_id, 'product_cat' );
        
        // check the categories for our desired one
        foreach ( $product_categories as $category ) {

            // if we find it, add the line item quantity to the category total
            if ( $category_id === $category->term_id ) {
                $category_quantity += $quantity;
            }
        }
    }

    return $category_quantity;
}

Now, let’s use this function to compare it against a minimum. You won’t have to change a single thing about the above function, as it’s going to be a helper for our next check. In the next function, you’ll adjust the minimum quantity and your desired category ID. We’ll compare our minimum to the category count in the cart, and throw our error notice if we haven’t met the minimum.

function sww_check_category_for_minimum() {

    // set the minimum quantity in the category to purchase
    $min_quantity = 6;

    // set the id of the category for which we're requiring a minimum quantity
    $category_id = 15;

    // get the product category
    $product_cat = get_term( $category_id, 'product_cat' );
    $category_name = '<a href="' . get_term_link( $category_id, 'product_cat' ) . '">' . $product_cat->name . '</a>';

    // get the quantity category in the cart
    $category_quantity = sww_get_category_quantity_in_cart( $category_id );

    if ( $category_quantity < $min_quantity ) {
        // render a notice to explain the minimum
        wc_add_notice( sprintf( 'Sorry, you must purchase at least %1$s products from the %2$s category to check out.', $min_quantity, $category_name ), 'error' );
    }

}
add_action( 'woocommerce_check_cart_items', 'sww_check_category_for_minimum' );

That's all we need! This function will pass the right information to our "get category quantity" function, so the only values you need to change at all are $min_quantity and $category_id.

Now if I have less than my minimum in the cart, the cart page will have a notice explaining what's required:

WooCommerce category minimum notice at checkout

WooCommerce helpfully notices that there's and error, and prevents checkout until whatever set off this error notice disappears.

WooCommerce category minimum notice

Once the category count meets my minimum, the notice is removed, and the customer can proceed with checkout normally.

WooCommerce category minimum met

You could certainly modify this for multiple product categories, or all product categories if desired. Feel free to borrow this code -- the full snippet is here -- or use it as a starting point. If you need help customizing it, I recommend Codeable for small customization jobs like this.


Thanks again for the question Duriez!

Have your own question? You can submit it here.

Articles you may also like:

Posted by Beka Rice

Beka Rice manages the direction of Sell with WP content and writes or edits most of our articles to share her interests in eCommerce. Or she just writes as an excuse to spend more time jamming out to anything from The Clash to Lady Gaga. Who knows.

2 Comments

  1. Is it possible to set this up to sell wine in ONLY groups of 6 and 12?

    1. Sure, see this section which outlines this exact scenario.

It's been over 2 years since this post was published, so comments have been closed. Thanks for reading!