Another interesting question I recently saw in a WooCommerce Facebook group post was where the shop owner wished to limit the number (quantity) per product a customer could add to the Cart and purchase. I decided to research it and come up with a solution. Keep reading and learn this one with me!
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
Note: All examples in this post are part of a development/learning WooCommerce install using the default Storefront theme. PHP code is placed in the child theme’s function.php file.
Perhaps this type of functionality is better suited for a single product or group of similar products instead of all the products in the online shop.
And that’s just what I did in this example, focus on a single product and limit the quantity of it you can add to the Cart for purchase.
The PHP code below will limit the number of Album products you can add to the Cart to 2:
add_filter( 'woocommerce_quantity_input_args', 'custom_woo_limit_quantity_single_product', 9999, 2 );
function custom_woo_limit_quantity_single_product( $args, $product )
{
if ( 24 === $product->get_id() ) {
if ( ! is_cart() ) {
$args['max_value'] = 2;
$args['step'] = 1;
} else {
$args['max_value'] = 2;
$args['step'] = 1;
}
}
return $args;
}
As shown in the following 2 screenshots, we cannot increase the quantity of the Album product beyond 2 on either the Single Product page or in the Cart:
However, with other products, we can increase the quantity beyond 2 on both the Single Product page and the Cart page:
Credit Resources
The epic Business Bloomer site has a fantastic piece published that I studied to help me figure out and understand how to achieve this functionality: https://www.businessbloomer.com/woocommerce-define-add-cart-min-max-incremental-quantities/.
If you see any mistakes in the code or know of a better solution, please share them freely in the comments section.
Thank you for reading this post. Please share it with someone else who would enjoy it as well.
Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.
Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.
More ways I can help
- Need hosting for your next web application or WordPress site? I highly recommend Hostinger and use them to host my niche bass fishing site. The service is second to none.
- 🔒5 Truths I’ve Come To Realize As a Self-taught Developer
- Desktop and mobile wallpapers, digital downloads, photography services, Shopify and WooCommerce customizations, and content writing – all in one E-commerce Shop. Find your next digital purchase today!
- Take your Laravel applications next level with Battle Ready Laravel by Ash Allen. Learn how to improve the performance, maintainability, and security of your Laravel projects in this e-book.
Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
📰 Get your brand, product, or service the attention it deserves with affordable classified ad placement in the OpenLampTech newsletter. I appreciate your support!
Great post on limiting the add to cart quantity in WooCommerce! I completely agree with your perspective that allowing unlimited quantities in the add to cart function can sometimes lead to unexpected outcomes, such as inventory issues or shipping complications. It’s great to see that WooCommerce provides an easy solution to this problem through the use of code snippets.
In addition to the snippet you provided, I have also found the “WooCommerce Min/Max Quantities” plugin to be a helpful tool for managing product quantities. This plugin allows for setting minimum and maximum quantity limits on individual products, as well as controlling the total quantity allowed in the cart.
Thanks for sharing your insights on this important topic for WooCommerce store owners. I will definitely be implementing your code snippet on my own site to ensure a smoother purchasing experience for my customers.
Thanks so much for your compliments. I’m glad you liked the article. I enjoy sharing what I learn about WooCommerce and PHP snippets with others when I learn them.
Great post on limiting the add to cart quantity in WooCommerce! I completely agree with your perspective that allowing unlimited quantities in the add to cart function can sometimes lead to unexpected outcomes, such as inventory issues or shipping complications. It’s great to see that WooCommerce provides an easy solution to this problem through the use of code snippets.
In addition to the snippet you provided, I have also found the “WooCommerce Min/Max Quantities” plugin to be a helpful tool for managing product quantities. This plugin allows for setting minimum and maximum quantity limits on individual products, as well as controlling the total quantity allowed in the cart.
Thanks for sharing your insights on this important topic for WooCommerce store owners. I will definitely be implementing your code snippet on my own site to ensure a smoother purchasing experience for my customers.