Limit WooCommerce order quantity by minimum / maximum

A handy code snippet to easily limit WooCommerce order quantity by minimum and maximum rules globally, or set different order quantity limits on a per product / product category basis.

Using this code snippet you should be able to easily limit WooCommerce order quantity by minimum / maximum per product, this could be combined with providing a list of product ids that you want to restrict, or limit the order quantity on a per product category basis.

Limit WooCommerce order quantity for all Products

Using WooCommerce built in woocommerce_add_to_cart_validation filter, we can validate the product being added to the cart by passing the name of our function and requesting that we get 3 arguments passed to our function.

add_filter('woocommerce_add_to_cart_validation', 'jcwcpq_woocommerce_product_min_max_validation', 10, 3);

Limit WooCommerce order quantity by minimum / maximum

Checking to see if the quantity is within the allowed range, if it is above or equal to the minimum requirement and below or equal to the maximum limit then we return true to say that this is a valid product.

// if product quantity is in allowed range
if ($quantity >= $min && $quantity <= $max) {
    return true;
}

Display WooCommerce notice if exeeding maximum order quantity

If the product quantity isnt within the allowed order quantity, we then check to see if it is above the maximum limit, then we return false to say its an invalid purchase and display a WooCommerce error message saying a generic contact us message.

// If selected quantity is to large, display message
if ($quantity > $max) {
    wc_add_notice('Please call our sales team for quantities larger than ' . $max . '.', 'error');
    return false;
}

Display WooCommerce notice if below minimum order quantity

Now if the user reaches this section of the function, it means that the product is below the minimum required quantity, we return false to say its outwith the quantity limits, and display a message to say the minimum order amount required.

// If selected quantity is to small, show message
wc_add_notice('A mininmum purchase limit of ' . $min . '.', 'error');
return false;

Example Code snippet

Putting all this code together, add this to your themes functions.php, changing the minimum or maximum order limits to what you required.

/**
 * Setting minimum and maximum quantity validation when adding products to cart.
 * 
 * @param boolean $bool Validation result
 * @param int $product_id Product id
 * @param int $quantity Quantity of item being added to the cart
 * 
 * @return boolean True or false
 */
function jcwcpq_woocommerce_product_min_max_validation($bool, $product_id, $quantity)
{
    $min = 2;
    $max = 10;

    // if product quantity is in allowed range
    if ($quantity >= $min && $quantity <= $max) {
        return true;
    }

    // If selected quantity is to large, display message
    if ($quantity > $max) {
        wc_add_notice('Please call our sales team for quantities larger than ' . $max . '.', 'error');
        return false;
    }

    // If selected quantity is to small, show message
    wc_add_notice('A mininmum purchase limit of ' . $min . '.', 'error');
    return false;
}
add_filter('woocommerce_add_to_cart_validation', 'jcwcpq_woocommerce_product_min_max_validation', 10, 3);

Limit WooCommerce order quantity per Product

If you want more control than globally setting WooCommerce minimum / maximum order quantity limit, we can customize this snippet to do it on a per product basis by passing the product id with its limits, we do this by replacing the following lines in the previously created function jcwcpq_woocommerce_product_min_max_validation.

$min = 2;
$max = 10;

With the following:

$product_limits = [
    // restrict product_id = 1 , minumim order of 2, maximum order of 10
    1 => [2, 10],
    // restrict product_id = 2 , minumim order of 5, maximum order of 10
    2 => [5, 10],
];

if(!isset($product_limits[$product_id])){
    return $bool;
}

$min = $product_limits[$product_id][0];
$max = $product_limits[$product_id][1];

Limit WooCommerce order quantity per Product Category

If you want to set WooCommerce order quantity limits on a per category basis, we can customize this snippet so that we check to see if the product belongs to any of the specified product categories, if it does then we set the minimum and maximum limits for that category. We do this by replacing the following lines in the previously created function jcwcpq_woocommerce_product_min_max_validation.

$min = 2;
$max = 10;

With the following:

$category_limits = [
    // restrict product_cat id = 1 , minumim order of 2, maximum order of 10
    1 => [2, 10],
    // restrict product_cat id= 2 , minumim order of 5, maximum order of 10
    2 => [5, 10],
];

$terms = wp_get_object_terms($product_id, 'product_cat', ['fields' => 'ids']);
if(empty($terms)){
    return $bool;
}

$found = false;
foreach(array_keys($category_limits) as $term_id){
    if(in_array($term_id, $terms)){
        $found = true;
        $min = $category_limits[$term_id][0];
        $max = $category_limits[$term_id][1];
        break;
    }
}

if(false === $found){
    return $bool;
}

Leave a Reply

Fields marked with an * are required to post a comment