In a WooCommerce store, managing how much of a single item a customer can buy is an important aspect of inventory control and marketing strategy. By setting a maximum purchase quantity, you can prevent stock hoarding, manage limited-edition items, or simply encourage broader distribution of a popular product. This tutorial will guide you through how to limit purchase quantity on a per-product basis using custom code in your theme’s functions.php
file.
Prerequisites
Make sure you have administrative access to your WordPress site and are comfortable editing PHP files. Implementing these changes in a child theme is recommended to preserve them during updates.
Step 1: Add Custom Code to Your Theme’s functions.php File
We’ll begin by defining the maximum purchase quantity for specific products. Open your theme’s functions.php
file and add the following PHP code:
/*
* Snippet: Limit Purchase Quantity in WooCommerce – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?972
* Tested with WooCommerce 8.8.2
*/
add_filter('woocommerce_quantity_input_args', 'wcsuccess_limit_purchase_quantity', 10, 2);
add_filter('woocommerce_available_variation', 'wcsuccess_limit_purchase_quantity_for_variations', 10, 3);
function wcsuccess_limit_purchase_quantity($args, $product) {
$limited_products = array(
25 => 5, // Product ID => Maximum quantity
30 => 3 // Product ID => Maximum quantity
);
if (array_key_exists($product->get_id(), $limited_products)) {
$args['max_value'] = $limited_products[$product->get_id()]; // Set maximum quantity
$args['min_value'] = 1; // Set minimum quantity
}
return $args;
}
function wcsuccess_limit_purchase_quantity_for_variations($variation_data, $product, $variation) {
$limited_products = array(
25 => 5, // Product ID => Maximum quantity
30 => 3 // Product ID => Maximum quantity
);
if (array_key_exists($variation->get_id(), $limited_products)) {
$variation_data['max_qty'] = $limited_products[$variation->get_id()]; // Set maximum quantity
$variation_data['min_qty'] = 1; // Set minimum quantity
}
return $variation_data;
}
Explanation of the Code
- Hooks: We use
woocommerce_quantity_input_args
to limit the quantity for simple products andwoocommerce_available_variation
for variable products. - Product and Variation IDs: You must know the IDs of the products or variations to which you want to apply the limit. These are specified in an associative array with the product ID as the key and the maximum quantity as the value.
- Setting Limits: The functions check if the product’s ID is in the array of limited products and, if so, set the maximum quantity the customer can purchase.
Step 2: Test Your New Limits
Once you’ve added the code:
- Visit your WooCommerce shop page or a product page with limits set.
- Try to increase the quantity of the limited product.
- Ensure that the quantity cannot exceed the maximum limit you have set.
Customising Further
You can easily adapt this code to include more products or to change the maximum limits as required. This makes it a versatile tool for managing sales during promotions or when launching new products.
Conclusion
Limiting purchase quantity in WooCommerce helps you manage your stock more effectively and ensures fair access to your products for all your customers. By implementing the “Limit Purchase Quantity” functionality, you can control product availability, manage special promotions, and prevent stockpiling.
Always test changes on a staging site before applying them to your live site to avoid any potential disruptions to your store’s operation. This guide on how to limit purchase quantity should help you enhance the shopping experience on your WooCommerce site, leading to better customer satisfaction and inventory management.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch