Count the Total Number of Checked Checkboxes with Gravity Forms

View this code snippet on GitHub.

Overview

There’s a classic Gravity Forms tongue twister that I love:

How many checks could a Checkbox count if a Checkbox could count checks?

Up until today, the answer to that question was 0 because it was entirely rhetorical. After today, that is no longer the case. The answer is now “all of them”.

This snippet gets the total number of checked Checkboxes and dynamically populates that total into a Number field on the form. You can use this to apply Conditional Logic based on the number of checkboxes checked, populate the number into a Quantity field to calculate Product purchases, and more! Let’s get started.

  1. Overview
  2. Using the Snippet
    1. Add a Number Field
    2. Update Snippet Parameters
    3. That’s It!
  3. Parameters
  4. Taking It Further
    1. Count as Quantities

Using the Snippet

Prerequisites

Confirm that you have Gravity Forms installed and activated and that you’ve installed the snippet.

Add a Checkbox Field

As the name of the snippet implies, we need a Checkbox field to count. Add one to your form.

Add a Number Field

Add a Number Field to the form.

The snippet accepts any field that supports numbers, such as Quantity fields. More on using the snippet to calculate Product Quantities in the Count as Quantities section below.

Update Snippet Parameters

Update the form_idcount_field_id, and checkbox_field_id in the snippet to match your Form ID, the Number field you’re populating with the count, and any Checkbox fields you want to count.

View this code snippet on GitHub.

That’s It!

Your form will now automatically count the number of checked boxes and populate it into the Number field.

Parameters

  • form_id (integer) (required) This is the Form ID that contains your Checkbox field. There is no default value.
  • count_field_id (integer) (required) This is the Field ID for the Number field in which the number of checked boxes should be dynamically populated. There is no default value.
  • checkbox_field_ids (array) (required) This is an array of Field IDs which should be counted. There is no default value.

Taking It Further

Count as Quantities

The snippet supports populating the Checkbox count into any field that supports Numbers, which includes Quantity fields. Paired with a Product field, the Checkbox count can be used for simple pricing calculations.

To do this, first add a separate Quantity field to your form and map it to your Product field.

With that place, configure the snippet to use the Quantity field for the count_field_id parameter. Once in place, any checked box automatically adds to the Quantity in the form.

Use GF Read Only to prevent users from overriding the populated Quantity.

Leave a Comment

Your email address will not be published. Required fields are marked *