• Home
  • Pricing
  • Free Resources
  • About
  • Docs
  • Support
  • Account

Gravity Wiz

Magically enhanced tutorials, snippets and plugins for Gravity Forms!

  • Gravity Perks
    • Gravity Perks
    • Pricing
  • Blog
    • Free Resources
    • About
  • Support
    • Docs
    • Support
    • Account

Subtotal Merge Tag (for Calculations)

A simple way of getting the subtotal for your Gravity Form calculations.

Last updated May 6, 2022 | Written by David Smith 363 Comments

  • November 4, 2019: Fixed notices caused by indirect modification of an overloaded element.

Stop! There's a better way.

This snippet is available as a plugin with Gravity Perks, a suite of over 46 premium Gravity Forms plugins!

  • View the Plugin
  • Buy Gravity Perks

Editor’s note: Our new eCommerce Fields plugin makes this 10x easier. 🪄

You’re welcome to use the snippet below, and we’re leaving this page live just in case. But you should know we’ve since released a new perk, Gravity Forms eCommerce Fields, that will easily handle all subtotal calculations out-of-the-box with no coding required.

Gravity Forms eCommerce Fields makes your forms eCommerce-friendly with support for Tax, Discounts, and Subtotal fields and includes a treasure trove of other magical eCommerce tools!

The snippet below was a great option before we released Gravity Forms eCommerce Fields. It may still be helpful to some, but it requires a bit of configuration and still does not come close to the level of functionality provided in our new perk. For example, the subtotals feature in eCommerce Fields helps your users better understand their orders in real-time, and can show the subtotal before shipping, coupons, and taxes have been applied.

Watch a quick overview:

Add eCommerce Fields to your Gravity Forms spellbook!

If you still want to download the Subtotal Merge Tags (for Calculations) snippet, read on and download it below. 

View DemoView CodeDownload Plugin

Prior to Gravity Forms eCommerce fields, there was no easy way to get the current total (let’s call it the subtotal) of the form for use in calculations. It was possible but you would have to enter all of your product merge tags into a long and complicated formula to get the subtotal.

This snippet helps you avoid that hassle by providing a {subtotal} merge tag for use in your Gravity Form calculations.

Please note: The {subtotal} merge tag is only available in fields that support calculations.

How do I install this snippet?

Easy peasy. Just copy and paste the code above into your theme's functions.php file.

How do I use this functionality?

This snippet will add a new “Subtotal” merge tag to the merge tag select available for the “Formula” setting on Calculation fields. Selecting this option will insert the {subtotal} merge tag into your “Formula” setting. You can then use all available mathematical operations available to this field to manipulate this subtotal as needed.

formula-subtotal-merge-tagFormula Setting: Subtotal Merge Tag

formula-subtotal-taxFormula Setting: Subtotal Used to Calculate Tax

Filed Under: Uncategorized

Comments

  1. Robert Chow says

    October 21, 2019 at 2:40 am

    Recently I noticed this error after either WP or GF got updated. If the disable the plugin, the error goes away. I’m using version 1.2 of the code.

    Notice: Indirect modification of overloaded element of GF_Field_Checkbox has no effect in /public_html/wp-content/plugins/gw-calc-subtotal/gw-calc-subtotal.php on line 123

    [redacted for brevity]

    Reply
    • David Smith says

      November 4, 2019 at 3:41 pm

      Fixed!

  2. Laritza says

    June 24, 2019 at 3:21 pm

    I need help! I have a form for which I have to duplicate the value of the first product is AM and PM are checked, then there is another option to select an additional product from a dropdown but this one is NOT duplicated even if AM and PM are checked. Is there any way I can do this? If I can do that then I could also add the subtotal paypal fee calculated field. I read the snippet and there is an option to not include a field in the subtotal calculation but I have another form using the subtotal field so that would not use the field number in the calculation and brake the other form. Thanks! Here is the form: http://yorkshireterrierclubofthenationscapital.org/index.php/specialty-shows-2019/

    Reply
  3. Conrad says

    May 1, 2019 at 2:21 pm

    I’m using Stripe as my payment processor for Gravity Forms. I need to add tax to my form, but my current setup just sends the total amount to Stripe, and I would have to figure out how much was tax separately. How does tax show up in Stripe through the ecommerce perk? Will it show up in Stripe’s tax field, or will it just show up as the total transaction amount?

    Reply
    • David Smith says

      May 2, 2019 at 9:16 am

      Support for mapping the Tax field to Stripe’s tax field is on our roadmap but not currently supported. For now, the Tax field is included in the cumulative total sent to Stripe.

  4. Jon Eynon says

    May 1, 2019 at 12:42 pm

    Does this subtotal save with the form? I am having issues with running some gv_math because I am trying to access the Totals from a form but it tells me that Totals are allowed in gv_math. I am wondering if this saves as a number or something that I can access later? My Total field is literally summing up 100s of items so to make a calculated # field is not possible given the form. Any thoughts?

    Reply
    • David Smith says

      May 1, 2019 at 1:06 pm

      The {subtotal} merge tag does not impact how the field data is saved. What you see on submission should be what is saved in that field. I haven’t used gv_match but I would not expect this functionality to impact whether or not that functionality works.

    • Jon says

      May 1, 2019 at 2:00 pm

      But are you saying that subtotal is saved with the form? Or is it just doing its thing on the live form and then doesn’t go anywhere once submitted? Gravity View Math doesn’t let us do math on the Total form field in Gravity Views so I am trying to come up with a way to get my Total over without it being incompatible. I was hoping this might be it. Does it save as a number in the database?

    • David Smith says

      May 2, 2019 at 9:18 am

      The value of the field is saved with the entry including whatever the calculated value of the {subtotal} merge tag. Including the {subtotal} merge tag in a calculation-enabled field does not impact how the calculation-enabled field works. If you Gravity View typically works with calculation-enabled fields, it should work with the calculation-enabled field that contains the {subtotal} merge tag.

  5. Sean Ramsey says

    March 29, 2019 at 3:15 pm

    Please help. I have a dev environment and am getting this error msg:

    Warning: Invalid argument supplied for foreach() in /srv/bindings/38c825ddfc6b4c72b486890a3c77ee96/code/wp-content/plugins/gravity-forms-add-subtotal-merge-tag-master/gf_subtotal.php on line 234

    This is not showing up in the Production environment.

    Reply
    • David Smith says

      March 29, 2019 at 3:52 pm

      More than likely a form you are referencing no longer exists.

      We recommend using the plugin version, Gravity Forms eCommerce Fields, which includes support if you’re still having issues.

  6. prince says

    March 26, 2019 at 9:49 am

    “Download Plugin” not working?

    Reply
    • David Smith says

      March 26, 2019 at 11:00 am

      Fixed. ?

  7. Aaron Rudd says

    February 8, 2019 at 4:29 pm

    I’m using this snippet for a donation form for my client.

    When any decimal appears after the dollar amount $10.(50cents), the Credit Card fields throws an error:

    This value must match the regex pattern. (/\A[a-zA-Z0-9_\-]+\z/ does not match for the value monthlyrecurring_40_1month_10.5).

    Any helpful ideas would be appreciated.

    Thank you!

    Reply
    • David Smith says

      February 9, 2019 at 2:02 am

      Hm, I’m not sure I understand? Is this caused by the {subtotal} merge tag?

    • Aaron Rudd says

      February 9, 2019 at 9:50 am

      My apologies. I needed to upgrade Stripe Add-On from 2.5 to 2.5.4. That fixed my issue. Thank you for responding.

  8. Layla B says

    January 16, 2019 at 3:00 pm

    I need some help installing this snippet with a skilled developer, can anyone help us update our gravity form with tax calculation subtotal issue as mentioned above? I can pay you of course for the help. Currently our form is not collecting tax and it’s a problem.

    Reply
    • David Smith says

      January 17, 2019 at 3:21 am

      Depending on the complexity of your needs, this plugin might be a much cheaper option than hiring a developer: https://gravitywiz.com/documentation/gravity-forms-ecommerce-fields/

  9. Chris says

    December 12, 2018 at 5:07 pm

    Sorry to bug you again. I updated to WordPress 5.0 and gravity forms 2.4.3. somehow now when i am using the subtotal merge tag, all the calculations are drastically off in the negative. I have 3 products, all with a price of 0, then an option field with checkboxes for each product, containing multiple selections with prices. On my tax calculation field (.0875*{subtotal}), if I select say a product that is $75 and a product that is $85, I get a calculation of -$85,414.00. if I change the calculation formula to only be {subtotal} and nothing else, the $75 and $85 product tally to Price: -$745,915.00. Could something have changed in the way this merge tag is tallying the subtotal? Thanks David!

    Reply
    • Jeff says

      December 17, 2018 at 9:47 am

      I am experiencing the same issue. I had to roll back to WordPress 5.0 and Gravity Forms 2.4.2. I experience the issue with WordPress 5.0.1 and Gravity Forms 2.4.2 and the problem seems to worsen with 2.4.3.

  10. Scott Carter says

    December 10, 2018 at 3:15 pm

    The newest version work with Gravity Forms 2.4.1 but breaks after updating to WordPress 5 with the following notice:

    The plugin gravity-forms-add-subtotal-merge-tag-master/gf_subtotal.php has been deactivated due to an error: The plugin does not have a valid header.

    Any thoughts on how to resolve this?

    Reply
    • David Smith says

      December 10, 2018 at 3:46 pm

      Hi Scott, I’ve added a proper plugin header to the snippet.

    • Scott Carter says

      December 10, 2018 at 10:13 pm

      Thanks, David!

  11. jocelyn says

    December 3, 2018 at 5:14 pm

    THANK YOU!!!

    Reply
    • David Smith says

      December 3, 2018 at 5:29 pm

      My pleasure, Jocelyn. ?

  12. Christopher Grasso says

    November 30, 2018 at 11:07 am

    Hi David, Im hoping you can help me out with this.

    i’ve began seeing a warning on all pages where i have a form, when this snippet is in my functions file: Warning: Invalid argument supplied for foreach() in /home/redacted/functions.php on line 597

    597 appears to be at the end of the snippet, located in the // check if form is passed section:

    foreach( $form[‘fields’] as $field ) {

    I updated gravity forms to 2.4 at some point this past week. I haven’t made any other changes to the functions file or gravity forms. The snippet was working fine for the past few months.

    Thanks Chris

    Reply
    • Lynne Arnold says

      November 30, 2018 at 3:11 pm

      It appears that this doesn’t work anymore with the latest version of Gravity Forms, 2.4.1. If there is a fix, I would love to know. Thanks, Lynne

    • Steve Wolfson says

      November 30, 2018 at 6:54 pm

      Hi, I’m having the same issue that Christopher Grasso reports. With Gravity 3.4.1 installed the Subtotals routine generates multiple error messages Warning: Invalid argument supplied for foreach() in /home/{redacted …}/functions.php referencing the line foreach( $form[‘fields’] as $field ) {

      I’ve restored the site to Gravity 3.3.6 and the form works correctly again. Thank you for assisting!

    • David Smith says

      December 1, 2018 at 7:45 am

      Hello all, this has been fixed. Grab the latest version of the snippet above.

    • Christopher Grasso says

      December 1, 2018 at 7:54 am

      Thank you David! confirmed working. I appreciate it, have a nice weekend.

    • Steve Wolfson says

      December 1, 2018 at 10:54 am

      Thanks very much David for the speedy update. Much appreciated!

    • David Smith says

      December 1, 2018 at 8:44 pm

      Glad to help! ?

  13. Frank Gomez says

    November 29, 2018 at 9:24 am

    Hi David, I want to be sure that a “SubTotal” field is “accessible” by the Stripe Addon in the Feed settings

    I want to create a subscription Subtotal and a Setup Fee subtotal and use the STripe Addon to create a feed that uses those subtotals Currently. The way I see it the Stripe Feed only accepts Product Fields or the Total. I want to be sure that the subtotal fields also work with the Stripe Feed.

    Reply
    • David Smith says

      November 29, 2018 at 9:55 am

      This snippet does not provide a Subtotal field, Frank. Are you referring to the GP eCommerce Fields plugin? If so, no, the Subtotal field is not available to Stripe. This is something we’d like to add support for in the future.

  14. Vaclav Navratil says

    November 28, 2018 at 5:13 pm

    I believe that this snippet stopped working with the release of the new 2.4 version.

    Reply
    • David Smith says

      December 1, 2018 at 9:23 am

      Fixed. Latest version above. :)

    • Edward Claughton says

      December 1, 2018 at 3:19 pm

      Hello,

      The sub-total is actually not quite fully working correctly. When I select the “products”I want to calculate in the sub-total, it calculates everything, not just what I selected. Note, the way I have the products structured is some of them had the same name although different product fields with different GF product numbers. I thought that might be causing it so I adjusted the names of the ones I want to include in the sub-total and still having the same issue. Could it be the names in the database are still the same or is this a bug?

      Thanks.

    • Edward Claughton says

      December 1, 2018 at 3:27 pm

      Disregard my last comment. sorry i figured it out!

  15. James says

    July 17, 2018 at 8:24 am

    Thanks for this. I used this solution to allow me to use the subtotal in a conditional statement for another field. Very helpful.

    Reply
    • David Smith says

      July 24, 2018 at 5:51 pm

      Glad it helped you!

  16. Arten says

    July 8, 2018 at 7:08 pm

    In general, I have a form http://35.226.46.43/create-shipment/, I need to pass it a little bit. make the connection between the areas allow delivery from MANHATTAN to MANHATTAN – $ 75 etc. And change the price calculation depending on the weight if more than 20 lb then + 0.75 $ per lb How can i do it? Thx

    Reply
  17. Ronny says

    June 28, 2018 at 1:20 pm

    I’m interested in passing the WooCommerce cart total into a Gravity Form via merge tag so that I can apply additional calculations based on user selections. Specifically, I’m trying to create a lease calculator that converts the cart total into a monthly lease price based on user selecting a lease type and lease terms. Based on the user selections, a lease rate would be multiplied by the cart total in order to calculate the monthly lease. Can this be done using Gravity Wiz?

    Reply
    • David Smith says

      July 5, 2018 at 10:51 am

      Hi Ronny, we don’t have a solution for this one.

  18. Janice says

    June 10, 2018 at 11:02 am

    Having a hard time showing taxes ony form, used the subtotal as mentioned above but it totals to a crazy large number. Unsure what I’m doing wrong. Help!

    Reply
    • David Smith says

      June 10, 2018 at 11:56 am

      Hard to say, Janice. If you’re a Gravity Perks customer, we’ll be happy to dig in via the support form.

  19. Mike says

    June 4, 2018 at 8:36 am

    I’m using this code to calculate a discount. It works perfectly (subtotal * number of checkboxes checked * discount percentage)

    The issue is that this “discount” is being added to the total, not subtracted from it. From what I can tell, there’s no way to subtract the total from the actual total, is there?

    Reply
    • David Smith says

      June 4, 2018 at 11:14 am

      You’ll need to add the discount as a negative amount in a Product field. That is the only way GF will pick up on it.

    • Mike says

      June 4, 2018 at 11:17 am

      How can I do that? The discount is a calculation, so I can’t add it as a negative amount.

      Or I can’t figure out how, anyway.

    • David Smith says

      June 5, 2018 at 3:27 am

      Add a negative sign in front of of your formula.

    • Mike says

      June 5, 2018 at 8:13 am

      That doesn’t seem to work.

      My calculation [(({subtotal}.05){Number:29})-({subtotal}*.05)] works perfectly to calculate the discount. (FYI, it’s 5% discount per add-on they check, which I’m pulling from the Number:29 field, which is a sum total of checkboxes checked, minus one discount for the initial add-on)

      If I add a – in front of the formula, the math somehow becomes wrong.

      No minus sign and the discount adds up to $256.25 (which is right; 25% of the total of all 6 add-ons – 5% per add-on after the 1st one)

      If I add a minus sign, it adds up to -$358.75, which is wrong.

      Same exact formula, but adding the – in front of it somehow breaks the equation.

    • Mike says

      June 5, 2018 at 8:14 am

      Aha!

      I added another set of parenthesis around the whole thing and now it’s calculating properly!

« Older Comments
Newer Comments »

Trackbacks

  1. Troubleshooting Issues with Calculation Results - Gravity Forms Documentation says:
    June 5, 2019 at 2:29 pm

    […] total field not being available is to use a Number field with a subtotal calculation. Refer to the GravityWiz Subtotal Merge Tag (for Calculations) article for more […]

    Reply

Leave a Reply Cancel reply

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

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Recent Posts

  • Our 46th perk is here. Say hello to Advanced Save & Continue.
  • Introducing Page Transitions 1.0 📖
  • Gravity Forms QR Code turns 1.0 🎉
  • How to Filter One Field By Another and Create Gravity Forms Chained Selects
  • Gravity Forms OpenAI

Categories

  • How To (78)
  • News (25)
  • Plugins (21)
  • Releases (19)
  • Resource (4)
  • Snippets (54)
  • Spotlights (18)
  • Tutorials (58)
  • Updates (177)

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2023 · Powered by WordPress · Gravity Wiz LLC

  • Support
  • Affiliates
  • About
  • Sitemap
  • Terms & Conditions of Use
  • Privacy Policy
  • Cookies Policy