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:

  1. The Form ID
  2. 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.

Dashboard listing forms with IDs emphasized for calculation reference

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.

Gravity Forms settings menu open, highlighting the Notifications option

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

Notifications settings page with an "Add New" button highlighted

Scroll down to the text editor and click on the merge tag button on the right. 

Gravity Forms interface showing Visual and Text tabs with an arrow pointing to code button

Now, select the name of the field you want to sum from the list.

Dropdown menu in Gravity Forms; user selects "Number of Attendees" field

You'll see the field merge tag appear in the text editor. Copy it to add to your shortcode.

Editor displaying field placeholder "{Number of Attendees:4}" in Gravity Forms

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.

Guests attending shown with a Gravity Forms math shortcode for calculating number of attendees

And on the front end, the shortcode is replaced with the sum of all values in the field.

Text shows guest count for a Space Station event and links to an online store for flight suits

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.

Add Widget options in Gravity Forms: Search Bar, Custom Content, Gravity Forms, and Page Size

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.

Using Gravity Forms shortcode to calculate total guest count

As you can see, the shortcode is replaced on the front end.

Total number of guests attending is shown as six, with buttons for copy, CSV, and print

To learn more about [gravitymath] and how to perform advanced calculations, read our ultimate guide to Gravity Forms calculations.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us