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 47+ essential add-ons for Gravity Forms with support you can count on.
Just used the provided snippet and I see one error: the form is correctly submitted, it “talks” to Stripe and gets back to the user visually presenting the custom message about the minimum quantity. However, the charge is made for the 1 piece in the quantity ( I set the minimum required quantity to 2 pieces).
Any advice?
Hi Val,
Does disabling Stripe fix the issue? This will probably require some digging. If it doesn’t work for you, then can you get in touch with us via our support form if you have an active Gravity Perks License.
Best,
Hi – Is this snippet still working with the new 2.5 release ?
I also use the GF ecommerce template form.
But the snippet has no effect.
Gravity Forms mentions not using the form specific version of filter with forms that also have payment add-on feeds and to use the generic version of the filter and perform the form id check within the hooked function. Would that involve adding an if $form[‘id’] = # statement within the filter code?
Hi,
You can add the if statement at the top of the code, to return if the $form[‘id’] != #. This way the codes attached to the filter will only run if the ID in the $form array matches a specific Form ID.
Best,
Hi, Worked perfectly for me The few points I would like to make:
Min Quantity message appears under each product box, When I have for example 10 products, its gives the message under each of them – not look nice. Can it be set to display on Top right after the main GF error message, as option?
In the beginning of the code as per bellow, $result = gw_validate_minimum_quantity – appears twice with the same ‘min_qty’ and ‘min_qty_fields’ options – what the second one does? do I need to change it as well to match the same as in first one?
add_filter(‘gform_validation_123’, function( $result ) {
} );
Hi Mr Pulman,
Displaying the validation error at the top will require some customizations to the snippet. If you have a Gravity Perks license, drop us a line via our support form and we’ll be happy to look into this. The $result = second gw_validate_minimum_quantity is applying a different minimum quantity to a different set of fields. You can ignore that part if you will be applying the same minimum quantity to all the fields on the form.
Best,
Hi, in my previous post you replied: “$result = second gw_validate_minimum_quantity is applying a different minimum quantity to a different set of fields.”
is that mean that it’s possible to set Quantity on All fields – AND – For individual Fields?
as for example if I want to set some fields with minimum Quantity of 2, some fields of minimum Quantity of 4, but in total all of the fields minimum quantity should be 12 ? Can I just add several $results ( third, forth…. etc. )
Hi,
Yes, you can add several $results. So you will add additional $result = gw_validate_minimum_quantity statement for every set of fields that has a similar minimum quantity.
Best,
I do need to set a minimum and maximum. And is there any way to have different error messages for each?
Hi Heath,
Maximum order quantity isn’t currently supported in the snippet. If you have a Gravity Perks license, drop us a line and we’ll be happy to look into adding support for this.
The last sentence of your article says “Need to set a maximum order quantity? Let us know and we’ll show you how to modify this snippet to do it!” Do I need a Gravity Perks license to get the modification?
Hello Heath, as Scott has explained before, the maximum order quantity isn’t currently supported in the snippet. With that being said, 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. Another way you can accomplish this is by using a separate Quantity field you can set a “max” range. If you do need added support for both fields, and you have a Gravity Perks license, you can drop us a line and we’ll be happy to look into adding this into the snippet.
Hi David,
Thanks for this great snippet. I have a question similar to Nin0 above (May 2019):
I have a form with 16 products (Christmas gift for employees/relations), all with a quantity field, and a filter activated to pre-fill the quantity fields with a 0, so visitors won’t have to do this manually for each product they don’t want to order.
How do I set the quantity validation for the entire form to a minimum of 1? And can I set the location of the validation message to be at the top of the form, just like a regular error message for invalid input (AJAX enabled, so will be visible straight away)?
Best regards, Gert-Jan
Hi Gerg-Jan, you would need to duplicate this for each quantity field:
http://snippi.com/s/obxi8xq
Is this plugin supposed to work with stock gravity forms, or does it require your plugins?
Thanks, Vi
Hi Vi, this is a stand alone snippet and only requires Gravity Forms.
Sweet! Thanks so much for publishing this helpful resource!
Happy to help, Vi. :)
Hello David,
I have a question about the “$min_qty” that we need to adjust to the minimum quantity. In your script I find 3 places to change this (2 in the filter, 1 in the function) Changing the one in the function doesn’t seems to do anything. Also the “min_qty_fields” can be changed in these 3 places, but only the one in the filter seems to have an effect on the front side.
Another issue is the one that Rasmus is referring to. What if you have 50 price fields? If one of these fields displays the error, all the other ones (that are empty) displays that error code. I discovered that you can avoid this by duplicating the “$result = gw_validate_minimum_quantity( $result, array(……” and then change the array by the number of the product field. But I guess this is not the proper when having 50 product fields or more. Is there a better way to solve this?
Thank you, Best regards Nin0
Hi Nin0, you only need to edit the code between lines 6 and 20. Nothing inside the
gw_validate_minimum_quantity()
function needs to changed.The issue is that you do need to let the user know which fields impact the minimum quantity. You could put a message at the top but that doesn’t accurately instruct the user on what needs to be interacted with in order to resolve the validation issue.
Hi David,
Thank you for the clarification. I have a better view on the procedure and how it works. However, If I have a minimum quantity of 5, that has to reflect on all the individual product fields when a number is filled in. Product fields that are not filled in (left blanc) should not display the error. So when I have 5 product fields and only the first one has a lower number than 5 (and the rest is not filled in) the error message should appear only in the product field that has that lower number. A viewer can choose the fields he wants to use an leave the remaining blanc. Is this possible?
Many thanks Best regards, Nin0
I wouldn’t expect the error message to appear on fields that have no quantity specified unless that quantity is specifically set to 0. If this is happening for you, this would be something we’d be happy to look into via support.
I think it checks the total field and not each product field for its quantity. When I set a minimum quantity of 5, this should apply to each individual product field and not only the total field. Ok for me to continue this via support, but I guess I have to purchase a license first. Is a basic account ok to help me fix these issues?
Hey Nin0, see the most recent update above regarding the new “validate_empty_fields” parameter. I think this will help you.
Hi David,
Thanks for the update but unfortunately it’s not working. I copied the new script and used the new parameter (set it to true and false) but it still displays the error message on all the other empty fields when only one field has a value. Also, as soon as one field has the minimum quantity, the error message is not being displayed if another field did not have the minimum quantity. It does not check each individual field but the total of all fields.
Best regards, Nin0
To clarify, It is supposed to check all fields. It’s a minimum order quantity rather than a minimum field quantity.
As for the validation message, this new version fixes that issue fo me. Validation messages are no longer showing up for me if the field has no value.
We’ll be happy to dig into this further but it would require and Advanced license (which includes snippet support). Our Basic license does not.
Hi David. Thanks for the snippet. If more than one field in the array, I seem to get the message for all products within the array. So if 1 product has error of “You need 5 more”, it also shows the message for the other products in the specified array. So it displays message on not-yet-filled-out quantity fields. Can you help me resolve? :-) Have a nice day, Rasmus
Hi Rasmus, this snippet is provided as-is; however, if you’re a Gravity Perks customer, this is something we’d be happy to take a look at via support. ?
Hi Rasmus, see the most recent update above regarding the new “validate_empty_fields” parameter. I think this will help you.
Hey David, I have several WC product pages with similar GF forms and they each have several product fields with minimum quantity for the total order. How would you suggest I modify this wonderful snippet to: 1) include the form ID function of several forms so I don’t have to repeat the snippet for each form and 2) where I have 3 product fields in the form with quantity enabled, how can I “sprintf” the min_qty_message once instead of appearing after each field when minimum quantity is not reached,
http://beta.dinnerup.com/product/acre-thai-bowl-buffet/
Thanks!
Hi Edward, we don’t have the throughput to provide support for snippet customizations but we’re happy to leave your comment here for any other folks who might like to take a stab at this. ?
How do you target a specific form with this code?
Hi David,
I tried to implement this snippet, but it does not work……
Have a look at the code:
http://snippi.com/s/v7fq405
You have to choose first “Zufuhr 24,50 €” in the first place, than
Kaminholz (25kg-Sack) ID 36 gets visible.
Can you help?
Thank you, Eva
I’ve updated the snippet with a few fixes. Try again. ?
Thankyou for sharing! Is there a way to modify this snippet for a minimum order price? (The kits we sell need to have a minimum $4000 combined to purchase) Have a good day!
We recommend Gravity Forms eCommerce Fields. Add a Number field to your form, use the special
{subtotal}
merge tag as the formula, then base the Submit button conditional logic on the value of that Number field.Is there any modified code you could recommend? :)
Hello, We’re getting an issue where the validation is working on the minimum order total and the form doesn’t actually get submitted or saves an entry but it is still charging the credit card. How would we overcome this?
Hi Dillon, what payment add-on are you using?
authorize.net
Hi We trying to run this code to limit the quantity in the product field. The code works fine but the one issue that we are facing is the page won’t load or give us an error message if the product field is not in the legal range. when we load the page and fill out the missing box we are supposed to get a message Variable value is not within the legal range.
what changes need to make so we get the result.
thanks
Hi Eddy, I’ve just tested the snippet locally and wasn’t able to recreate this issue. If you’re a Gravity Perks customer, we’ll be happy to provide additional support via the support form.
Thanks for the great share.
We are still receiving the ‘Oops! We could not locate your form.’ error.
Do you have any suggestions?
Thanks again!
This typically occurs when the form ID is not correct.