Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How To Implement Minimum and Maximum Quantity Rules for Specific Products in WooCommerce?

If you own an online store, you may have an ‘N’ number of reasons to limit the product quantities. One instance is that, if you are running a promotional sale, you can set limits to certain products that are part of a promotion to ensure that customers don’t buy too low or too many of these products.
The code snippet will set a minimum quantity limit of 2 and a maximum quantity limit of 5 for certain products defined in the code with their specific product IDs.

function ts_woocommerce_quantity_input_min_callback( $min, $product ) {
    // Array of product IDs for which to set the minimum quantity
    $product_ids = array( 100, 470, 457 ); // Update with your desired product IDs
    
    // Check if the current product ID is in the array
    if ( in_array( $product->get_id(), $product_ids ) ) {
        $min = 2; // Set the minimum quantity to 2 for the specified products
    }

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

/*
* Changing the maximum quantity to 5 for specific WooCommerce products
*/

function ts_woocommerce_quantity_input_max_callback( $max, $product ) {
    // Array of product IDs for which to set the maximum quantity
    $product_ids = array( 100, 470, 457 ); // Update with your desired product IDs
    
    // Check if the current product ID is in the array
    if ( in_array( $product->get_id(), $product_ids ) ) {
        $max = 5; // Set the maximum quantity to 5 for the specified products
    }

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

Output

When customers select products with the product IDs defined in the code (100, 470, and 457), these specific products will only be allowed to be selected within a specified quantity range of 2 to 5. This means that customers can select a minimum of 2 of these products and a maximum of 5. Any attempt to select a quantity below 2 or above 5 for these products will not be permitted.

How To Implement Minimum and Maximum Quantity Rules for Specific Products in WooCommerce? - Tyche Softwares


Similarly, you can set quantity step increments and restrict the quantity field to selected numbers in WooCommerce.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.