How to Calculate the Sum of a Field in Gravity Forms
Using the [gravitymath] shortcode you can calculate the sum of a field for all entries in a form.
Prefer to Watch the Video?
We need two things to find the sum of a field for all form entries:
- The Form ID
- The Merge Tag of the field we want to sum
The shortcode will look like this:
[gravitymath scope="form" id="4"] {merg tag:2} [/gravitymath]
scope="form"
tells the shortcode that we’re working with data from a form (as opposed to a View). The id
value refers to the ID of your form in Gravity Forms and the merge tag should be the merge tag of the field you want to sum. Merge tags are always wrapped in curly brackets.
Note: The default behavior for [gravitymath]
is to output a sum. If you want to output a count instead, you can use the :count
operator, like this: {merge_tag:2:count}
.
Finding Your Form ID:
Hover over Forms in your WordPress admin menu and click on Forms, you’ll see the form ID in the first column on the right.
Finding the Field’s Merge Tag
Here's an easy way to find the merge tag for any field in your form.
From the form editor, hover over Settings at the top and click on Notifications.
Now, click Add New to create a new notification (we’re not going to save it, we’re only using this as a way to get the merge tag!).
Scroll down to the text editor and click on the merge tag button on the right.
Now, select the name of the field you want to sum from the list.
You'll see the field merge tag appear in the text editor. Copy it to add to your shortcode.
Displaying the Sum of a Field on Your Website
You can add the [gravitymath]
shortcode anywhere on your website!
As you can see, here we’ve added it to a new page.
And on the front end, the shortcode is replaced with the sum of all values in the field.
Adding the Shortcode to a View
You can add the [gravitymath]
shortcode to a View by pasting it inside a Custom Content field or widget.
To do this, go to your View editor, click the Add Widget button and select Custom Content.
Next, open the widget settings by clicking on the gear icon. Now, paste the shortcode inside the text editor along with any other text/HTML you want to display.
As you can see, the shortcode is replaced on the front end.
To learn more about [gravitymath]
and how to perform advanced calculations, read our ultimate guide to Gravity Forms calculations.