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!
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:
If you still want to download the Subtotal Merge Tags (for Calculations) snippet, read on and download it below.
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.
{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.


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!
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.
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
I’d give GF eCommerce Fields a try. It supports a much improved method for calculating the subtotal.
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 :-(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.
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?
Hi Maggie, what are the actual contents of that line on your end?
On my end, line 446 is this: mergeTags[“pricing”].tags.push({ tag: ”, label: ” });
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
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).
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?
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. :)
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
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.
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.
My pleasure, Mische. Glad you were able to get this working. :)
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
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.
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
Hi Mische, we do not currently have a solution for this one. Leaving this open for any other users who might have a suggestion.
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.
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.
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!
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?
We are using Gravity Forms PayPal Standard Add-On. The plugin is update but still unable to redirect to paypal checkout page.
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.
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?
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.
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
This might help, Eddie: https://gravitywiz.com/documentation/snippet-troubleshooting/
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!
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?
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.
This was a great help. Thank you so much!
Glad to hear it, John!
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!
Awesome, glad you were able to make this work for you. :)
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.
Could you send me an export of your form?
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.
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.