I recently had a Gravity Forms user ask the following question: “How can I add a “0” in the quantity field by default.”
Out of the box, Gravity Forms does not support setting a default quantity for the quantity field available in the Product field, by default the quantity box is empty.
This can be problematic, especially if you have multiple product fields in your form that are required. The user may think leaving the quantity field empty is the same as 0, but an empty quantity field will cause a validation error.
If you can populate the quantity field by default with any number, in this case, the request is to set the quantity to zero, you can avoid the validation errors and in some cases maybe even ensure the user is making the appropriate selections.
There are two ways of accomplishing this.
Option 1: Using the Quantity Field (no code)
If you’re confused, let me clear this up for you.
Gravity Forms has a stand-alone Quantity field that can be mapped to your Product fields so that you can disable the quantity field in the Product field.
There, that should have cleared it up 🤔.
But just in case you’re still (or more confused), here are some steps to take:
- Go to Pricing Fields Section
- Drag the Quantity Field into your form
- Click the Product field and check “Disable quantity field” (in the General settings)
- Click the new quantity field to open the General settings
- Under “Product Field Mapping” Select the product you want to map the field too
Once the new Quantity field is mapped you can set the default quantity by using the Default Value option in the Advanced field settings.
- Click on the Quantity Field
- Go to the Advanced tab
- Enter a number in the Default Value field
See it in action:
Option 2: Using a Code Snippet
Even though there is a little bit of code involved, you might find that this solution is easier and more convenient. It will allow you to use the quantity field that is in the Product field reducing the number of fields in your form.
Not to mention, because the code snippet is provided, you only need to cut and paste to get this magic to work!
- Copy the code below
Where to put this code? You will need either edit your functions.php file and paste this code there, or use a functionality plugin like WPCodeBox (recommended).
- Paste it into your functions.php file or functionality plugin (I will be using WPCodeBox for this)
- Click your Product field to open the settings
- Go to the Advanced tab
- Check the box “Allow field to be populated dynamically”
- Add this parameter “default_quantity” to the Quantity field
The Wrap Up
That’s it. You should now be able to set the default quantity of your product field. My only suggestion is that you use the solution that is best for you.
If you have any issues or questions, please feel free to use the comments below.