Set minimum order quantity per category in woo commerce store without plugins

GOKUL G. NAIR
4 min readJun 30, 2022

--

In many cases you may want to limit the quantity of product per purchases on your eCommerce business in several cases. Adding constraints may help increase profitability and operational effectiveness in a variety of business designs. This is especially true if you own a wholesale business or have a one-of-a-kind manufacturing method. However, how can you integrate this capability into your Woo Commerce store?

That's why, in today’s tutorial, you’ll learn how to set minimum and maximum order quantity in Woo Commerce. Setting a Woo Commerce minimum order amount is a great way to improve your revenue since it requires your consumers to add more items to their cart in order to reach a certain order number, after which they can go to the checkout page. So, let’s get started!

Benefits of setting minimum and maximum order quantity in Woo Commerce

You may wish to apply maximum and minimum quantity constraints to your Woo Commerce store for various reasons. All eCommerce websites have high backend costs such as processing time, shipping charges, and warehousing and distribution fees. As a result, low order quantities are frequently loss-making. So, in Woo Commerce, you can define a minimum order quantity. You can assure that ALL orders, regardless of amount, are lucrative.

Applying a maximum quantity or order value prevents clients from placing orders that might deplete your inventory. Either that or undercut your rates. Changing the standard quantity in Woo Commerce encourages consumers to buy more from you. In a primary Woo Commerce store, the default is set at zero, enabling buyers to place minor orders. Customers may automatically order three products if you increase that to a default of three (or even more). Because they’d think that’s what other people who visit your online business are doing.

To implement the Woo Commerce minimum order amount on your website, we need to add some new code to the Theme Functions. It is strongly advised that you build a child theme and enable it on your Woo Commerce site to prevent theme issues when modifying. To enter the Theme Functions, navigate to Appearance > Theme Editor from your WordPress dashboard.

Then you’ll be sent to the theme editor page, where you’ll see the Theme Functions section on the right side of the screen (also named functions.php File).

Lets assume that I have different product categories on my store. I have a “plant” category that I’d like to allow clients to order ONLY if the number of “plant” items in the cart is > 3 .

In order to do so copy and paste the following code in my functions.php :

add_filter(‘woocommerce_quantity_input_args’, ‘woocommerce_quantity_changes’, 10, 2);function woocommerce_quantity_changes($args, $product)
{
if (!is_cart()) {
if (is_singular(‘product’) && (has_term(‘plant’, ‘product_cat’))) {
$args[‘input_value’] = 3; // Start from this value (default = 1)
$args[‘max_value’] = 10; // Max quantity (default = -1)
$args[‘min_value’] = 3; // Min quantity (default = 0)
$args[‘step’] = 1; // Increment/decrement by this value (default = 1)
}
}
return $args;
}
add_filter(‘woocommerce_quantity_input_args’, ‘min_qty_filter_callback’, 20, 2);
function min_qty_filter_callback($args, $product)
{
$categories = array(‘Plant’); // The targeted product category(ies)
$min_qty = 3; // The minimum product quantity
$product_id = $product->is_type(‘simple’) ? $product->get_parent_id() : $product->get_id();if (has_term($categories, ‘product_cat’, $product_id)) {
$args[‘min_value’] = $min_qty;
}
return $args;
}
// On shop and archives pages
add_filter(‘woocommerce_loop_add_to_cart_args’, ‘min_qty_loop_add_to_cart_args’, 10, 2);
function min_qty_loop_add_to_cart_args($args, $product)
{
$categories = array(‘plant’); // The targeted product category
$min_qty = 3; // The minimum product quantity
$product_id = $product->get_id();if (has_term($categories, ‘product_cat’, $product_id)) {
$args[‘quantity’] = $min_qty;
}
return $args;
}
add_action(‘woocommerce_check_cart_items’, ‘custom_set_min_total’);
function custom_set_min_total()
{
if (is_cart() || is_checkout()) {
global $woocommerce, $product;
$i = 0;
foreach ($woocommerce->cart->cart_contents as $product) :
$minimum_cart_product_total = 3;
if (has_term(‘plant’, ‘product_cat’, $product[‘product_id’])) :
$total_quantity += $product[‘quantity’];
endif;
endforeach;foreach ($woocommerce->cart->cart_contents as $product) :
if (has_term(‘plant’, ‘product_cat’, $product[‘product_id’])) :
if ($total_quantity < $minimum_cart_product_total && $i == 0) {
wc_add_notice(
sprintf(
‘<strong>A Minimum of %s products is required from the plant category before checking out.</strong>’
. ‘<br />Current number of items in the cart: %s.’,
$minimum_cart_product_total,
$total_quantity
),
‘error’
);
}
$i++;
endif;
endforeach;
}
}

--

--

GOKUL G. NAIR

Mechanical Engineer by Qualification Software Engineer By Passion | Entrepreneur