Skip to content

Add Quantity Fields to Gravity Forms Image Choice Cards

Put a quantity box inside each Gravity Forms Image Choice card so people can pick a product by its picture and say how many, using the Image Choice Quantities snippet from Gravity Labs.

Chris Eggleston 4 min read

The Problem

A customer asked me this one: they were using the Image Choice field so people could pick products by photo, and wanted a quantity box on each card. Pick the Sunrise Blend, say how many bags, pick the Midnight Espresso, say how many of those.

Gravity Forms doesn’t do that out of the box. The Image Choice field only records which cards were picked. You can add Number fields for the quantities, but they sit underneath the whole field, away from the pictures, and people have to match “Quantity 2” to the second card in their head.

The Solution

Gravity Forms has a snippet for this on Gravity Labs, Image Choice Quantities. You still add a normal Number field for each card, and the snippet moves each one inside its card when the form loads. It’s done on the server, so there’s no flash of the fields jumping into place, and because they are still real Gravity Forms fields, validation, conditional logic, entries and merge tags all work as usual.

A Coffee Order form with three Image Choice cards. Sunrise Blend and Midnight Espresso are selected and each card has a Bags number field inside it, set to 3 and 2. Canyon Roast is not selected and shows no field.
The finished form: a Bags field inside each selected card.

Step 1: Build the Form

  1. Add an Image Choice field and add your choices (I used three coffees with Select Multiple, so people can order more than one).
  2. Add one Number field for each card, in the same order as the cards. The Field Label shows inside the card, so keep it short: “Quantity” or “Bags”.
  3. Give each Number field an Admin Field Label with the product name (Advanced tab), like “Sunrise Blend bags”. Otherwise every quantity in your entries and notifications is called “Bags”.
  4. Optional but nice: add conditional logic to each Number field, Show this field if Choose Your Coffee is Sunrise Blend, so the box only appears once its card is selected. Set a Default Value of 1 if you want it to start there.
  5. Note the IDs: the form ID, the Image Choice field ID, and each Number field’s ID. Click a field and the ID is in the top right of Field Settings.
The Gravity Forms editor with the Image Choice field and three Bags number fields below it. The first Bags field is selected and the Field Settings panel shows ID: 2.
In the editor the Number fields stay below the cards. The first one is field ID 2.

Step 2: Work Out the Choice Numbers

The snippet matches each Number field to a card by its choice number, and the numbering depends on the Image Choice field’s selection mode:

Selection1st card2nd card3rd cardNotes
Select Multiplechoice_1choice_2choice_3Skips numbers ending in 0: the 10th card is choice_11
Select Onechoice_0choice_1choice_2Starts at 0

If you’re not sure, preview the form, right-click a card and choose Inspect. The card’s class ends in its number: gchoice_108_1_2 is form 108, field 1, choice 2.

Step 3: Add the Snippet

Get the code from the Image Choice Quantities page on Gravity Labs. Log in with your Gravity Forms account to see it, then click Copy code.

Add it to your site with a code snippets plugin like WPCode Box (as a PHP snippet that runs everywhere), or paste it into your child theme’s functions.php.

The settings are in the $gf_image_choice_quantities array at the top of the snippet. For the coffee form they are:

SettingWhat it isCoffee form
form_idYour form’s ID108
image_choice_field_idThe Image Choice field’s ID1
quantitiesOne line per card: the choice number and the Number field that goes in itchoice_1 → 2, choice_2 → 3, choice_3 → 4

Replace the example values with yours. You can map more than one Image Choice field on the same form (add another entry under field_mappings) or more than one form (add another entry to the top list). A card with no line in quantities simply gets no box.

Step 4: Test It

Preview the form, select a couple of cards, change the quantities and submit. Clicking in a quantity box doesn’t select or deselect the card around it, and the entry stores each quantity in its own Number field:

Entry 739 in Gravity Forms: Choose Your Coffee lists Sunrise Blend and Midnight Espresso, Sunrise Blend bags is 3 and Midnight Espresso bags is 2, followed by the name and email.
The entry: the coffees picked, then a quantity for each, named by their Admin Field Labels.

Things to Know

  • Only on the front end. In the form editor and on the entry screen, the Number fields show in their normal place, so you can still edit them.
  • Card order matters. Choice numbers follow the order of the cards. If you drag a choice to a new position, update the mapping.
  • Required quantities and conditional logic play well together. A required Number field hidden by conditional logic isn’t checked, so people only have to fill in the boxes for the cards they picked.
  • These are quantities, not prices. The Number fields don’t add anything to a total. If each card is a product with a price, see Gravity Forms Checkbox Products for ways to sell several products from one field.
  • Not in a mu-plugin as is. The snippet runs as soon as it’s loaded and needs Gravity Forms to be loaded first. That’s the case in functions.php and in WPCode Box; if you use a mu-plugin, load the snippet inside add_action( 'gform_loaded', ... ).

Download the Form Template

It’s free. Get the Coffee Order form from this help doc, with the setup notes, ready to import.

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

Need More Help?

If you still need help, use the chat or leave a comment below and we’ll get back to you.

Comments

Ask a question or leave a comment

Your email is never shown. For help with a specific form, share what you expected and what happened instead.