• Home
  • Pricing
  • Free Resources
  • About
  • Docs
  • Support
  • Account
Hello Gravity Shop 👋 • Gravity Forms inside WooCommerce products. • Learn More

Gravity Wiz

Magically enhanced tutorials, snippets and plugins for Gravity Forms!

  • Gravity Perks
    • Gravity Perks
    • Gravity Shop
  • 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 47 premium Gravity Forms plugins!

  • View the Plugin
  • Buy Now

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. Stefani says

    November 27, 2017 at 4:30 pm

    Hi there! Thank you so much for this snippet! I placed in the php file and see the {subtotal} merge tag. But, it doesn’t seem to be working. I have a box for donors to check if they want to include the merchant fee for PayPal. If they check the box, the Product field comes up called Merchant Fee, and in that field, I have a calculation. I’ve tried (({subtotal}*2.2/100) +0.30), ( {subtotal} * 0.022 ) + 0.30) and even just {subtotal} * 0.022, but no matter what I use, nothing is showing in the Merchant Fee field. Here is the URL – the form is in the blue bar (sorry about the music auto-playing): https://gbgrr.org/tree-of-hope/

    I’d appreciate ANY extra sets of eyes! Thank you!

    Reply
    • David Smith says

      November 29, 2017 at 7:58 am

      Hi Stefani, the demo for this snippet appears to be working still. If you’re still having issues, I would recommend checking out Gravity Forms eCommerce Fields which provides this same functionality (and much more) as a plugin; no code required.

  2. hannansoft says

    July 5, 2017 at 8:06 pm

    Hi it is a good snippets but when i use subtotal tag , my form doesnt perform well on mobile phones. how can i fix it? thanks

    Reply
    • David Smith says

      July 14, 2017 at 12:49 pm

      I’d give GF eCommerce Fields a try. It supports a much improved method for calculating the subtotal.

  3. Chris says

    March 24, 2017 at 8:29 am

    Hello David! Does anyone commented that this tag doesn’t work on the email notifications or Gravity PDF templates?

    When I use the {subtotal} tag, it outputs nothing on the HTML template for the email or PDF :-(

    Reply
    • David Smith says

      March 24, 2017 at 2:08 pm

      Hi Chris, this snippet is limited to the formula setting of a calculation-enabled fields. If you’d like a more flexible (and powerful) version, check out GP Ecommerce Fields.

  4. Maggie Cameron says

    February 6, 2017 at 12:30 pm

    Hello, I’ve been using this very successfully for quite a while now, and it’s still seems to be working. However, when I view my form entry, I now get this message:

    Header information – headers already sent by (output started at /home/girlcook/public_html/wp-content/themes/parallax-pro/functions.php:446) in /home/girlcook/public_html/wp-includes/option.php on line 828

    and

    Header information – headers already sent by (output started at /home/girlcook/public_html/wp-content/themes/parallax-pro/functions.php:446) in /home/girlcook/public_html/wp-includes/option.php on line 829

    Line 446 is of course part of the Subtotal Merge Total code. Any ideas what’s happening?

    Reply
    • David Smith says

      February 8, 2017 at 11:16 am

      Hi Maggie, what are the actual contents of that line on your end?

    • Maggie Cameron says

      February 8, 2017 at 5:53 pm

      On my end, line 446 is this: mergeTags[“pricing”].tags.push({ tag: ”, label: ” });

  5. Daniel Helen says

    February 1, 2017 at 9:23 am

    I have a form which offers multiple payment options, with a setup similar to the one described here: https://gravityplus.pro/how-to/setup-multiple-payment-options-on-one-gravity-form/

    For the transaction fees, each payment type has its own product field with a calculation – using the subtotal tag – that shows/hides according to some conditional logic rules.

    While the seems to work fine in Chrome (though it is a little slow when I try to select items from drop-downs), the JavaScript crashes completely in Firefox.

    You can recreate the issue here: https://www.tolkiensociety.org/events/seminar-2017/book/beta/

    This is error message:

    http://snippi.com/s/1vz74bm

    Reply
    • David Smith says

      February 1, 2017 at 11:33 am

      Hi Daniel, the {subtotal} merge tag does not perform well when used multiple times on the same form. If you’re attempting to add any fee, we recommend using a Tax field instead (via the GP Subtotal & Tax perk; available by request to Gravity Perks license-holders).

    • Daniel Helen says

      February 1, 2017 at 11:41 am

      Hi David, thanks for you reply. Is there no workaround given that only one field using the {subtotal} tag is visible at any one time?

    • David Smith says

      February 1, 2017 at 11:46 am

      I’m sure there is but it would require customizing the snippet to handle it. While the snippet is still free and publicly available, I’ve invested in a much better solution with GP Subtotal & Tax. :)

  6. Chris says

    January 30, 2017 at 12:53 pm

    Hi David,

    I have GP installed and am trying to perform a calculations which I need your help with please. I have a number of products in Group A which I want to apply discounts/loadings to and get a total. I then have another Group B which I perform separate discounts or loadings to to create another total which I add to the first and then add tax

    All the products are on the same page. The subtotal snippet seemed like a solution but I can’t seem to create 2 separate sub totals and then add them together

    Any ideas greatly appreciated! Thanks in advance

    Reply
    • David Smith says

      January 30, 2017 at 1:57 pm

      Hi Chris, if you’re a Gravity Perks user, get priority support here: https://gravitywiz.com/support/ We have a new perk coming out very soon which we’d love for you to take for a spin. I think it’ll be a great fit for what you’re trying to do here.

  7. Mische says

    January 26, 2017 at 2:53 pm

    I figured out the issue – for some reason the fact that the Subtotal in form #1 is a Price Field makes it angry. As soon as I change the Subtotal field in form #1 to a number field, all of the experimental, dynamically populated, data-hungry fields I’ve built in form #2 come alive with form #1’s subtotal.

    So my grand plan for anyone else trying to come up with a discount solution not using Coupons: (yet to be tested but still a plan) 1. Gather subtotal from form #1 2. In form #2, bring the Subtotal in but as a Product Field this time. 3. Add a User Defined Product Field (that only the Admin can edit) so the Admin can enter whatever total dollar amount they want to discount. 4. Add the code from example #1 on the GravityHelp forum. To remove negative product issue. https://www.gravityhelp.com/documentation/article/gform_paypal_query/

    Hopefully that’ll work out. For my situaion, simply using the code in #4 in the main form won’t work. My client actually waives the lower of 2 fees – when those fees are waived and how much is waived are both conditions that are all over the place so I’d have to create about a thousand coupons and my client’s team would have to keep track of them… no good. So I thought, what if the Subtotal becomes my highest priced product? Since I don’t need form #1 to actually go to Paypal based on my client’s workflow, there’s no reason the Subtotal can’t be a Product (and the highest priced one) on another form.

    David, thank you again. You rock. Honestly.

    Reply
    • David Smith says

      January 26, 2017 at 8:18 pm

      My pleasure, Mische. Glad you were able to get this working. :)

  8. Marko Sustic says

    January 26, 2017 at 5:51 am

    Hello David,

    First of all thanks for the awesome snippet. It saved me multiple times and im using it without problems for a long time now.

    Recently i noised in my cPanel that my errorLog was edited and when i looked in to it i saw that it errors out on 36th line of code of this snippet. I will provide the exact line in the code area.

    the error is something like “invalid argument supplied at line …” and the line is in the code below.

    so foreach gets wrong arguments.

    Is this a common problem? How do i and do i have to fix it?

    I will add that the snippet work without problems. It just puts this error messages in my log file every day a few times.

    Thanks in advance

    I have new version od wordpress installed and new version of gravity forms

    Marko Sustic

    Reply
    • David Smith says

      January 27, 2017 at 9:21 pm

      Typically this occurs when Gravity Forms attempts to load a form that does not exist. Most often a user error when an incorrect form ID was specified for a shortcode. If you’re able to recreate the error consistently with a valid form, let me know.

  9. Mische says

    January 22, 2017 at 4:34 pm

    Hi David. Thanks for another fantastic & helpful tool.

    Can you see a way to use the subtotal field to act as a step to subtrack from the total before submitting to PayPal? I know that question is not specific at all – I basically need exactly what his person did (https://www.gravityhelp.com/forums/topic/remove-validation-for-negative-price ) but don’t have the chops to pull off the hook you mentioned, so I’m trying to find a work-around that avoids the negative price error from Paypal. (my client has too many possible pricing & fee reduction scenarios just like that guy’s situation)

    I’m having an oddly tough time with such a simple concept. It would be amazing to have: [subtotal] [field for subtracted amount that the client can use] [reason for reduction so it’s on record, again to be filled in by client abt their customer] [total that’s sent to Paypal]

    It would look like: Subtotal $4700.00 – $1200.00 [Shoreline Stabilization fee waived – Application includes New Structure as well] Total: $3500

    Fingers crossed that you can help!

    Mische

    Reply
    • David Smith says

      January 23, 2017 at 7:54 am

      Hi Mische, we do not currently have a solution for this one. Leaving this open for any other users who might have a suggestion.

    • Mische says

      January 24, 2017 at 2:54 pm

      Hmm. I’m trying to get crafty with this and I may have a solution. But first, can you help me get the Subtotal from form #1 to form #2? If this thing I’m trying to do works, I swear I’ll share my solution in great detail.

      Here’s the skinny: In form #1, my {subtotal} is calculating in a Product field. In form #2, I’m trying to dynamically populate another product field with form #1’s subtotal. To do this I tried adding a parameter name to the field in each form, then went to the confirmations and tried to pass it as a query string. (it doesn’t have to be a product field in form #1 – it could be a number field just as well. But in form #2 it does have to be a product)

      I’m definitely missing somthing critical.

    • David Smith says

      January 24, 2017 at 5:55 pm

      If you’re passing the value of Field A from Form 1 in the query string with a query string parameter of “&fielda={Field A:9}” (pretending that 9 is your field ID), and on the Field B on Form 2 you have the dynamic population parameter set as “fielda”, the value from the query string should be populated into that value.

  10. Mi says

    January 9, 2017 at 9:10 am

    Awesome plugin btw.

    I have question about integrating paypal checkout with the subtotal. The paypal extension we are using only takes payment amount from the pricing fields. I was able to use product field with your extension’s calculator but after clicking submit, the page would not redirect to the paypal check out page.

    When I select payment amount on other product field and total field, it works. Please advise.

    Thanks in advance!

    Reply
    • David Smith says

      January 10, 2017 at 8:42 am

      Hi Mi, I’ve tested this with the official Gravity Forms Paypal add-on and it works as expected. What add-on are you using?

    • Min says

      January 10, 2017 at 10:13 am

      We are using Gravity Forms PayPal Standard Add-On. The plugin is update but still unable to redirect to paypal checkout page.

    • David Smith says

      January 10, 2017 at 5:12 pm

      We’d recommend picking up a copy of Gravity Perks and dropping us a request for GP Subtotal & Tax. If it doesn’t work with the plugin-version, we’ll be happy to provide support.

  11. Andrew says

    January 5, 2017 at 5:34 pm

    Hi David,

    if I wanted two fields one to add a ‘processing fee’ of 2% of the product price and then a field to add 20% VAT but not include the processing fee would I need to get the Gravity Perks licence to enable me to do that easily?

    Reply
    • David Smith says

      January 5, 2017 at 9:44 pm

      Hi Andrew, the snippet version does not support having multiple product-based {subtotal} merge tags. It creates an infinite loop. The plugin version includes a Tax field which is more performant and provides the ability to exclude specific products from the calculated total.

  12. Eddie says

    December 21, 2016 at 10:26 am

    thanks for the snipped, but for some reason as soon as i add it to my themes function file, the site breaks :( i see this message on the log file

    PHP Parse error: syntax error, unexpected ‘<‘ /themes/Minimal-child/functions.php on line 17 [21-Dec-2016 15:00:20 UTC] PHP Parse error: syntax error, unexpected ‘<‘ in /themes/Minimal-child/functions.php on line 17

    Reply
    • David Smith says

      December 21, 2016 at 11:16 am

      This might help, Eddie: https://gravitywiz.com/documentation/snippet-troubleshooting/

    • Eddie says

      December 21, 2016 at 11:57 am

      That did the trick! thank you! it was the PHP opening tag that was messing it up…one i removed that it worked like a charm!

  13. Todd Dengler says

    December 18, 2016 at 5:26 pm

    Hey guys Great post, however, I can’t seem to get this to work with the Coupon Add-on. When I add the 7% tax to the Tax field, the tax after discount still calculates for the full price, not the reduced price after the coupon. What am I doing wrong?

    Reply
    • David Smith says

      December 18, 2016 at 10:09 pm

      Hi Todd, GP Subtotal & Tax provides support for a Tax field that will automatically exclude the discounted amount from the calculated tax. This perk is available to Gravity Perks license holders by request.

  14. John Kelly says

    December 14, 2016 at 12:35 pm

    This was a great help. Thank you so much!

    Reply
    • David Smith says

      December 19, 2016 at 9:06 pm

      Glad to hear it, John!

  15. Lynne Arnold says

    December 9, 2016 at 9:16 pm

    I got the same error that the subtotal gave a really weird amount – it was almost twice as much but not quite, so I don’t know how it calculated it. So I just changed the calculation to just show the fee itself. So mine is not entitled “Sub-total” but just “Fee.” Then the GF total is correct.

    Thank you for this REALLY helpful solution!

    Reply
    • David Smith says

      December 10, 2016 at 1:21 pm

      Awesome, glad you were able to make this work for you. :)

  16. Lev says

    November 15, 2016 at 11:30 am

    Hi It is very strange that after implementing subtotal TOTAL field now showing double amounts what do I did wrong I have a promo code that activates 10% discount So here are the fields Cost– $ 80.00 10% discount -$ 8.00 Subtotal should show $78.00 So is the total should show $78.00 it shows $156.00 ( double) So where do I go wrong.

    Reply
    • David Smith says

      November 15, 2016 at 5:43 pm

      Could you send me an export of your form?

  17. Simon says

    November 10, 2016 at 4:26 am

    Do you have a solution for VAT (UK 20%) when a coupon is used?

    I have a situation where I have created a subtotal the traditional way using the long list of product prices and then *0.2 but this does not subtract the value of the coupon which for arguments sake is $25. So the actual tax total is not the sum of the products minus the coupon *0.2 which for VAT purposes it is supposed to be.

    Reply
    • David Smith says

      November 10, 2016 at 10:59 am

      Hi Simon, GP Subtotal & Tax provides support for a Tax field that will automatically exclude the discounted amount from the calculated tax. This perk is available to Gravity Perks license holders by request.

« Older Comments
Newer Comments »

Trackbacks

  1. Building A WordPress Donation Form Using Gravity Forms, MailChimp and Stripe – INN Labs says:
    November 16, 2017 at 10:57 am

    […] Forms Add Subtotal Merge Tag (This is an INN fork of Subtotal Merge Tag and must be installed […]

    Reply
  2. Gravity Forms Pricing: Adding Tax – Gravity Forms Documentation says:
    October 5, 2017 at 3:35 pm

    […] Subtotal Merge Tag (for Calculations) via GravityWiz.com […]

    Reply
  3. Building A WordPress Donation Form Using Gravity Forms, MailChimp and Stripe | INN News Nerds says:
    February 14, 2017 at 1:51 pm

    […] Forms Add Subtotal Merge Tag (This is an INN fork of Subtotal Merge Tag and must be installed […]

    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

  • New: Gravity Forms Google Sheets 1.0
  • Spotlight: How Gravity Forms OpenAI is Helping Animals Find Fur-ever Homes
  • How to Dynamically Populate Drop Down Fields in Gravity Forms
  • How to Populate Database Data into Gravity Forms
  • AlbumBlitz: Searching Through the World’s Music with Advanced Select

Categories

  • How To (92)
  • News (25)
  • Plugins (21)
  • Releases (26)
  • Resource (5)
  • Snippets (55)
  • Spotlights (27)
  • Tutorials (76)
  • Uncategorized (1)
  • Updates (202)
  • Workshops (2)

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