May 17, 2019: Add "validate_empty_fields" parameter and set it to true by default. This parameter controls whether fields with not value should show the minimum quantity validation message.
Gravity Forms provides the ability to set a minimum per field when using a separate Quantity field but what about a minimum quantity for the entire order? This snippet can help!
How do I install this snippet?
Click the “Copy to Clipboard” link at the bottom of the snippet then paste the code into your theme’s functions.php file. Abracadabra!
Do I need to configure this snippet to work with my form?
Yes! This snippet has four bits to customize.
- Update the
86
in the filter namegform_validation_86
to the ID of your own form. - Update the
$min_qty
variable to the minimum quantity your form should require. - Update the
$min_qty_message
variable to the validation message which should be displayed if the minimum quantity is not met. Use%1$d
to populate the minimum number of tickets require. Use%2$d
to populate the number of tickets the user must add to their order to meet the minimum. The actual values will be populated when the form is validated.Minimum Quantity Validation Message
- If you wish to limit the minimum quantity requirement to a specific set of fields, enter the IDs of these fields in the
$min_qty_fields = array();
array like so:$min_qty_fields = array(3, 5);
. Otherwise, simply leave this variable as is.
Ready to go!
And that’s it; you’re all ready to go. If you use this snippet, we’d love to see it live on your site. Consider sharing a link to your implementation in the comments.
Need to set a maximum order quantity? Let us know and we’ll show you how to modify this snippet to do it!
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 46+ essential add-ons for Gravity Forms with support you can count on.
For a minute I thought I had found a solution but the script is returning an “Oops! We could not locate your form” error!
Try now, Lucie. Snippet has been updated.
Hi David,
I am trying to set this up for different forms, with different minimum quantities for various fields. What would the best way be to set this up?
Hi Helen,
You can bind to multiple forms like so:
Hi David,
I’ve been trying to get this to work with Woocommerce and Gravity Product Add ons but it is still submitting the product to the cart with though my product quantities are zero. I guess the Woocommerce button is overriding the Gravity forms quantity however, I have 2 other fields that are required such as date and a select field and the validation rules run if they are not selected.
Any suggestions?
Hi James, I would expect this snippet to work with the WC GF Product Add-ons plugin; I’m not sure why it wouldn’t. Maybe another user has encountered this same issue.
This snippet is exactly what I needed, should be built into Gravity Forms. Been trying to solve this myself for hours.
Wonderful!
James
Lovely! Glad we could help. :)
Hey David,
I am using Ecwid my e-commerce through word press. For some items I need the minimum quantity ordered. Do you know how I can integrate the code with Ecwid?
Best, Sam
Hi Sam, sorry, I do not. :(
Hello, Do you think its possible to use conditional logic on another field (a text field for example) , but based on the quantity?. For example to display a field if the quantity is bigger than 3?, something like that?. I tried using normal conditional logic but apparently quantity fields are not in the option for conditional logic. Thanks
Hi Jose, you can capture the quantity field in a Number field with Calculations enabled and then based the conditional logic on that field.
Hi there, i’m using this little snippet for the first time and it’s exactly what i needed however, using $min_qty_fields = array(124, 137, 233); will validate all ID’s when only one ID is filled in.
Is it possible to only validate the ID that’s been filled in?
Thanks in advance Sander
Hi Sandor, if you include multiple fields, they’re considered part of the same “group” and so if one fails validation, they will all fail validation. You can, however, copy and paste this snippet multiple times to configure each field individually. Just make sure you rename the function (i.e. gform_validate_minimum_quantity becomes gform_validate_minimum_quantity1, gform_validate_minimum_quantity2, etc).
Hello, I want to set a minimum price (and not disable the submit button if the price is not high enough).
Let’s say users are paying for stuff, and if the total is under 30 usd, then it’ll display 30 usd and the user will have at least to pay 30 usd.
Is there any way to do that?
Regards,
This was exactly what I needed! I’m using this on a order form with independent, and optional, products. Is there a way that if the quantity is left blank then the snippet will be nulled? Currently it’s forcing the customer to order the minimum amount even if they don’t need the product.
Thanks!
Hi Danny, sorry for the slow response. I’ve just updated the snippet to address this issue.
Not a problem at all! I appreciate you updating the code, thank you!!
Hi.
First, thank you very much for this snippet.
Please let me know if is possible to set a minimum quantity for two products, for example: We have Product A + Product B and we want to set a minimum quantity of 20 for both, like 10 products A + 10 products B, or 5A + 15B or something else.
Thanks :)
Hi Goncalo, this snippet only supports setting a minimum order quantity; however, you can create separate Quantity fields for each product and there is an option to set a minimum for each there.
Hey there, I’m looking for a way to set a total form price minimum. I have multiple items, and have already set minimum quantities. Now I would like the form to not be able to be submitted before a total of $300 is chosen from my products.
Would modifying this code be a good place to start? Thanks for any help you can give!
Hi Cliff, use this snippet to add a Subtotal to your form. You can then use conditional logic to show/hide the submit button based on the value of your calculated subtotal field.
https://gravitywiz.com/subtotal-merge-tag-for-calculations/
Is it possible to modify this to force a maximum quantity on a specific field?
I have a form that I don’t want a client to be able to checkout if there’s more than 2 in the quantity field.
I tried to figure it out, but couldn’t.
Hi Mike, if you use a separate Quantity field you can set a “max” range (screenshot).
My problem is that I’m using a Product field (not just a Quantity field), so I don’t have options for the max value.
Hoping I can adapt this script to close that loophole.
I think I don’t need to now in playing with this.
I’ll map a quantity field to the Product field and do my shipping calculation math on that.
Thanks!
Perfect. That is what I was thinking. :)
This is exactly what I needed! Thank you for this, it’s working as it is supposed to. I would like to know if it is possible to have one field that increases the quantity by more increments. Example: Donut Flavor1 = +/- 1 Donut Flavor2 = +/- 1 Assorted Dozen Donuts = +/- 12
I have my minimum set to 12 (dozen donuts minimum order) But I also have a field that lets people order a full dozen (called assorted dozen). This field obviously costs more, but only increases the calculation of this script by 1. I would like to tell the script that if this ONE field is greater than 1, that means they have met the minimum requirement of 12 donuts. If there is a way to increase the total by increments of 12 when this field is used, that would work best.
Hi Adam, you might find this snippet more aligned to your needs: https://gravitywiz.com/rounding-increments-gravity-forms/
Hi David
Thanks for the code. A few issues.
I wanted to apply a minimum quantity for a few fields. I added multiple field ids in the array section. It works except that even if the quantity field is blank its still requests that minimum quantity. The way i’d like it to work is, if the field is blank do nothing but if the quantity is greater than zero but less than minimum order quantity display error.
Also, it seems to tally the entire array of fields rather than individual fields. So if the minimum quantity set is 20 and three fields have quantities of 3 it says “You must order a minimum of 20 . Order 11 more to complete your order.” Rather than saying for each field “You must order a minimum of 20 . Order 17 more to complete your order.”
Thanks!
The main thing is how do I exclude a blank field (quantity of zero)? But if soemone adds a value thats less than minimum quantity but greater than blank (zero) display error.
Hi David – Nice Snippet!
Is there a way to modify this to set a minimum total for the amount? I am setting up a form that lets you customise your own subscription pack, made up of lots of little add-ons – but it needs to be above a certain dollar amount before it is worthwhile.
Thanks!
Hi Jason, Richard (from Gravity Forms) wrote a handy snippet that lets you apply conditional logic based based on the form total. If you need to apply conditional logic based on the total of a specific field and it’s options, you can use a Number field with calculation enabled to calculate that total and apply the conditional logic based on that Number field.
how must I change the code to use it with more than one form? I have tried to put the code just 2x times in the function.php, but then I get a ERROR;-)
Hi Jesper, this resource might help: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/ Keep in mind, you’ll only include one copy of the class itself and then you can instantiate it multiple times.
Hi David, thanks, I do not know how to use this link to class-based snippet ? I have change the ID in the first line: add_filter(‘gform_validation_8’, ‘gform_validate_minimum_quantity’);
The second form have id=23, but how can I the use the code class-based, to include this form id too?
Best Regards;-)
Sorry, Jesper. Forgot this is not a class-based snippet (one of my older ones). You’d need to duplicate all the code and change the form ID in the filter name. I would also consider adding the form ID to the function name to more easily distinguish them. Here’s a simplified example:
http://pastie.org/9810069
Perfect, now I understand;-) I have now 4 forms with this snippet, working perfect! Thank you so much David.
Glad to help, Jesper. :)
Great little snippet. I applied this to a specific set of fields but even if the quantity is zero the error comes up. How do I set it that the error comes up ONLY if the user enters a value? IE a blank field or zero value is ok but if they enter any other number (greater than zero but less that minimum value) the error is shown
Cheers!
Works great. Thanks for this code.
What if you wanted the function to appear on multiple fields BUT only if they fill the field out? At the moment a visitor isnt required to fill out the field (ie zero) but if they enter a value less than four but greater than blank (zero) the error should come up. At the moment it comes up on everything (event the things they dont want to order)
Here is a screenshot: http://i.imgur.com/rrBgwIh.png
Hey,
Could you let me know how to adapt this snippet, so that I can set a maximum order quantity.
Thanks a lot!
Hi Jennifer, the simplest way (assuming you don’t need to set a minimum quantity) would be to change this line
if($quantity >= $min_qty)
to this:if($quantity < $min_qty)
. This would effectively change the behavior of this snippet to enforce a maximum rather than minimum quantity.How if I want to modify this snippet, so it limit maximum and minimum in number / other field based on user meta
What I want is to make withdraw points form, and maximum limit amount is based an user meta field