Recently I came across an interesting problem while creating a Holiday Catering ordering system for Gus’s Market using WooCommerce. The page uses the WooCommerce One Page Checkout extension as well as a custom checkout template to move the checkout onto a second page. The page also has multiple products with minimum quantities.
Unfortunately the Min/Max Quantities extension is not fully compatible with One Page Checkout at the time of writing, so that wasn’t an option. (It requires that every product with a minimum quantity be ordered. So if you’ve got a minimum quantity of 5 pounds for the roast beef, you can’t just not order roast beef even though you’re a vegetarian and just wanted 10 pounds of mashed potatoes.)
In looking for quick, existing code to use online, I came across several references to a custom spyr_set_min_qty_per_product function which I’ve included below for search purposes. Do yourself a favor and keep reading however, as the code doesn’t work correctly.
The code almost works correctly – in that it works occasionally based on having multiple “bad products” in your cart at the same time. We’ve fixed the issue and provided updated, working code below following the same logic:
- Go through the products in the cart.
- Check if any of them requires a minimum quantity.
- When a product below the minimum quantity is found, add it to a list of “bad” products.
- If the “bad” list contains at least one product, show the error message.
Feel free to let us know if you find the code useful or have any questions!