Nested calculations in GravityMath
A [gravitymath] shortcode can hold other GravityMath shortcodes in its formula. GravityMath calculates each inner shortcode first and puts its result into the outer formula as a number. Each inner shortcode has its own scope, id and filter, so one result can combine totals from different forms, or from the same form with different filters.
Write a nested calculation #
Use [gravitymath2] for the shortcodes inside [gravitymath]. This adds the hours logged in a support tickets form (form 27) to the minutes of a consulting calls form (form 29), turned into hours:
[gravitymath decimals="2"] [gravitymath2 scope="form" id="27"] {Hours:3:sum} [/gravitymath2] + [gravitymath2 scope="form" id="29"] {Minutes:2:sum} [/gravitymath2] / 60 [/gravitymath]With 22.5 hours of tickets and 150 minutes of calls, it shows 25.00.
Combine filters on the same form #
Inner shortcodes can read the same form with different filters. This bills Standard tickets at $80 an hour and VIP tickets at $120 an hour, from the Plan field (field 2) and the Hours field (field 3):
Total due: $[gravitymath decimals="2"] [gravitymath2 scope="form" id="27" filter="filter_2=Standard"] {Hours:3:sum} * 80 [/gravitymath2] + [gravitymath2 scope="form" id="27" filter="filter_2=VIP"] {Hours:3:sum} * 120 [/gravitymath2] [/gravitymath]With 10 Standard hours and 12.5 VIP hours, it shows Total due: $2,300.00. Math filters explains the filter parameter.
Things to know #
- An inner shortcode can’t use the same name as the shortcode around it. In
[gravitymath] [gravitymath scope="form" id="27"] {Hours:3:sum} [/gravitymath] + 1 [/gravitymath], the first closing tag ends the outer shortcode, so no number shows and the rest of the formula appears on the page as text. - You can go three levels deep:
[gravitymath], then[gravitymath2], then[gravitymath3]. - The outer shortcode can have its own
scopeand merge tags, as in[gravitymath scope="form" id="27" decimals="2"] {Hours:3:sum} + [gravitymath2 scope="form" id="29"] {Minutes:2:sum} [/gravitymath2] / 60 [/gravitymath]. - Put
decimalson the outer shortcode to round the final result.