Feature Highlight: Creating complex calculations using nested shortcodes

Feature Highlight: Creating complex calculations using nested shortcodes in GravityMath

Written by Casey Burridge Marketing Coordinator at GravityKit since 2021, Casey is an expert on Gravity Forms, WordPress, and marketing.

Last updated:

Categories GravityMath

Tags

In this edition of Feature Highlights, we’re exploring nested calculations using GravityMath. This new feature allows you to perform advanced calculations on your Gravity Forms data using multiple [gravitymath] shortcodes. 

With nested calculations, you can take Gravity Forms calculations to the next level! Keep reading as we show you how to nest calculations using the [gravitymath] shortcode.

The [gravitymath] shortcode

GravityMath is an add-on for Gravity Forms that allows you to perform advanced calculations on your form data using a shortcode. The [gravitymath] shortcode works anywhere on your website including in pages, posts, Views and widgets!

The [gravitymath] shortcode allow you to write any kind of math calculation. You can include Gravity Forms field values in your calculations using merge tags. All you need to do is set the scope parameter to “form” and specify the form ID.

Here’s an example of a calculation using the GravityMath shortcode.

[gravitymath scope="form" id="52"] ( {Number:1} + {Number:3} ) / 365 [/gravitymath]

GravityMath also includes the ability to nest multiple shortcodes. This allows you to create more complex calculations and pull entry data from different forms.

What are nested calculations

With GravityMath you can nest multiple GravityMath shortcodes. In other words, you can add a [gravitymath] shortcode inside another [gravitymath] shortcode! This allows you to perform more complex calculations. Here are some examples of what you can do:

  • Use data from different forms in one single calculation
  • Use calculated values inside a calculation
  • Calculate percentages, totals, averages, and more

One thing to note is that you can’t use the same shortcode inside itself, so when nesting calculations, be sure to use [gravitymath2] and [gravitymath3] instead!

Use cases for nested calculations in GravityMath

Let’s look at some practical example of how to use nested calculations on your website. 

Example #1: Billing for support requests

In this example, we’ll write a calculation that calculates a bill for support requests. We have two forms on our site, one for support questions, and another for VIP support requests. Regular support questions are billed at $100, while VIP requests are billed at $200.

The below shortcode will calculate the total for each type of support request and add them together.

[gravitymath] 

  [gravitymath2 scope="form" id="12"] {Support Questions:2:count} * 100 [/gravitymath2] 

+ [gravitymath2 scope="form" id="4"] {VIP Support Requests:4:count} * 200 [/gravitymath2]

[/gravitymath]

As you can see, we’ve set the “scope” to “form” as we’re working with all values in a specific form.

Example #2: Calculating a quote total

In this example, we’re creating a quote by adding up the amount charged for different services (CSS styling, theme customization, and content uploading). We’ve set the “scope” parameter to “entry” because the data is all coming from the same form submission. 

[gravitymath] 

  [gravitymath2 scope="entry" id="6"]] {CSS styling:3:value} * 400 [/gravitymath2] 

+ [gravitymath2 scope="entry" id="6"] {Theme customization:4:value} * 350 [/gravitymath2]

+ [gravitymath2 scope="entry" id="6"] {Content upload:4:value} * 200 [/gravitymath2]

[/gravitymath]

Example #3: Calculating donation amounts

This is going to be our most complex example, but it’s also a good indication of how powerful nested calculations can be! 

In this example, we’re going to calculate the total amount donated to a nonprofit for different causes. The nonprofit has two donation forms on their site—one for one-off donations through PayPal or Stripe, and one for donations towards specific causes, such as Science or Education.

In this example, we’re adding up the amounts donated via Stripe and PayPal and then adding that total to the amounts donated towards science, education and medicine. 

[gravitymath]

[gravitymath2]

[gravitymath3 scope="form" id="105"] {PayPal donations:4:sum} [/gravitymath3]

+ [gravitymath3 scope="form" id="112"] {Stripe donations:7:sum} [/gravitymath3]

[/gravitymath2] +

[gravitymath2 scope="form" id="101" filter="filter_5=Science"] {Donation amount:1:sum} [/gravitymath2]

+ [gravitymath2 scope="form" id="101" filter="filter_5=Education"] {Donation amount:1:sum} [/gravitymath2]

+ [gravitymath2 scope="form" id="101" filter="filter_5=Medicine"] {Donation amount:1:sum} [/gravitymath2]

[/gravitymath]

As you can see, there are really no limits to what you can do using nested calculations.

Get started with nested calculations today!

GravityMath is a powerful add-on for Gravity Forms that allows you to perform calculations on your form data using a shortcode. By nesting multiple shortcodes, you can create complex calculations that allow you to use values from different forms.

If you’re new to GravityMath, and want to learn more about performing calculations in Gravity Forms, check out our ultimate guide to Gravity Forms calculations!

Helpful tips right in your inbox.

Subscribe to our biweekly newsletter for tips, special offers, and more!

Helpful tips right in your inbox.

Subscribe to our biweekly newsletter for tips, special offers, and more!