Skip to content

How to Set Minimum and Maximum Product Quantities in WooCommerce

By ali 5 min read

How to set minimum and maximum product quantities in WooCommerce

WooCommerce allows customers to choose how many units of a product they want to purchase. By default, the quantity selector does not always have the minimum and maximum limits a store owner needs.

For example, you may want customers to purchase at least one unit of every product and limit each order line to a maximum of five units. You can control these values using WooCommerce quantity filters.

In this guide, you will learn how to set minimum and maximum product quantities for all WooCommerce products and how to apply different limits to a specific product.

WooCommerce Filters for Minimum and Maximum Quantities

WooCommerce provides two filters to change the minimum and maximum values displayed in the product quantity selector:

  • woocommerce_quantity_input_min: Changes the minimum quantity value.
  • woocommerce_quantity_input_max: Changes the maximum quantity value.

Both filters receive the current quantity limit and the product object. This allows you to apply the same limit to every product or use product details to set different limits.

You can find the quantity input implementation in the official WooCommerce quantity input template reference.

1. Set the Minimum Quantity for All Products

To require customers to select at least one unit, use the woocommerce_quantity_input_min filter.

Add the following code to your child theme’s functions.php file or a custom plugin:

/**
 * Set the minimum product quantity to 1.
 */
function geekerhub_set_minimum_product_quantity( $min, $product ) {
    return 1;
}
add_filter(
    'woocommerce_quantity_input_min',
    'geekerhub_set_minimum_product_quantity',
    10,
    2
);

This code changes the minimum quantity value to 1 for all products where the WooCommerce quantity input uses this filter.

Customers will not be able to select a quantity below one through the standard quantity selector.

2. Set the Maximum Quantity for All Products

You can use the woocommerce_quantity_input_max filter to set the maximum quantity customers can select.

For example, the following code limits the quantity to two units per product line:

/**
 * Set the maximum product quantity to 2.
 */
function geekerhub_set_maximum_product_quantity( $max, $product ) {
    return 2;
}
add_filter(
    'woocommerce_quantity_input_max',
    'geekerhub_set_maximum_product_quantity',
    10,
    2
);

After adding this code, the standard WooCommerce quantity selector will use 2 as the maximum value.

This is a per-product quantity input limit. It does not automatically mean that a customer can purchase only two units across the entire cart if the same product can be added through other methods or custom integrations.

3. Set Both Minimum and Maximum Quantities

You can combine both filters to set a minimum of one unit and a maximum of five units for every product.

/**
 * Set minimum product quantity to 1.
 */
function geekerhub_set_minimum_quantity( $min, $product ) {
    return 1;
}
add_filter(
    'woocommerce_quantity_input_min',
    'geekerhub_set_minimum_quantity',
    10,
    2
);

/**
 * Set maximum product quantity to 5.
 */
function geekerhub_set_maximum_quantity( $max, $product ) {
    return 5;
}
add_filter(
    'woocommerce_quantity_input_max',
    'geekerhub_set_maximum_quantity',
    10,
    2
);

With these filters in place, the standard quantity selector will show a minimum value of 1 and a maximum value of 5.

4. Set Minimum and Maximum Quantities for a Specific Product

You may not want to apply the same quantity limits to every product. For example, one product may have a maximum quantity of two, while another can be purchased in larger quantities.

You can check the product ID inside the filter callback and return different values for the selected product.

Replace 123 with your actual WooCommerce product ID:

/**
 * Set the minimum quantity for a specific product.
 */
function geekerhub_product_minimum_quantity( $min, $product ) {
    if ( 123 === $product->get_id() ) {
        return 2;
    }

    return $min;
}
add_filter(
    'woocommerce_quantity_input_min',
    'geekerhub_product_minimum_quantity',
    10,
    2
);

/**
 * Set the maximum quantity for a specific product.
 */
function geekerhub_product_maximum_quantity( $max, $product ) {
    if ( 123 === $product->get_id() ) {
        return 5;
    }

    return $max;
}
add_filter(
    'woocommerce_quantity_input_max',
    'geekerhub_product_maximum_quantity',
    10,
    2
);

In this example, product ID 123 has a minimum quantity of two and a maximum quantity of five. Other products keep their existing quantity limits.

If you are adding several custom WooCommerce features, you can keep the code organized in a small custom plugin instead of placing every snippet in your theme. See our guide on how to create a custom plugin in WordPress.

Important Notes

  • Maximum value: A maximum value of -1 is commonly used to indicate that there is no fixed maximum quantity.
  • Product stock: The available stock can also affect the quantity a customer can purchase. A custom maximum should not be treated as a replacement for WooCommerce stock management.
  • Cart and checkout: These filters control the quantity input values. If your store has strict purchase limits, add server-side validation for cart additions and quantity updates as well. Do not rely only on browser-side input limits.
  • Variable products: Test the limits on product variations as well as simple products, especially if your store uses custom product templates.

For example, if you already use custom rules to control payment methods, you may also want to review our guide on disabling Cash on Delivery for specific WooCommerce products.

Conclusion

You can set minimum and maximum product quantities in WooCommerce using the woocommerce_quantity_input_min and woocommerce_quantity_input_max filters.

Use these filters to apply quantity limits to all products or check the product ID to set different limits for individual products. If the limits are important to your store’s ordering rules, make sure you also validate quantities on the server side and test the cart and checkout process.