How to Set a Default Product Quantity in Gravity Forms

THIS POST MAY CONTAIN AFFILIATE LINKS. PLEASE READ OUR DISCLAIMER FOR MORE INFO.

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:

  1. Go to Pricing Fields Section
  2. Drag the Quantity Field into your form
  3. Click the Product field and check “Disable quantity field” (in the General settings)
  4. Click the new quantity field to open the General settings
  5. 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.

  1. Click on the Quantity Field
  2. Go to the Advanced tab
  3. 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!

  1. Copy the code below
<?php
// Copy everything below this line
add_filter( 'gform_field_value_default_quantity', 'gr_default_qty' );
function gr_default_qty() {
return 0; // change this number to your desired default quantity
}

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).

  1. Paste it into your functions.php file or functionality plugin (I will be using WPCodeBox for this)
  2. Click your Product field to open the settings
  3. Go to the Advanced tab
  4. Check the box “Allow field to be populated dynamically”
  5. 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.

Get the latest news, tips, and how-tos all delivered to you!

Your information is used only for email communications by GravityRanger. We do send the occasional product promotion email.

Name*
This field is for validation purposes and should be left unchanged.


>