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!
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 David,
I have been using this snip it for a while, it has always worked great. PayPal recently reached out and had us switch our gateway to Gravity Forms PayPal Checkout Add-On, instead of the now legacy Gravity Forms PayPal Payments Pro Add-On. For some reason, the {subtotal} merge tag now breaks the form, it won’t submit with this tag active on a product calculation field. This is the vague message I got back from PayPal support: Within the API request history, I found requests with status codes 400 and 422. 422 indicates UNPROCESSABLE_ENTITY and 400 indicates Bad Request. The 422 was due to content within the request that did not follow the required logic: “issue”: “AMOUNT_MISMATCH”, “field”: “/purchase_units/@reference_id==’default’/amount/value”, “description”: “Should equal item_total + tax_total + shipping + handling + insurance – shipping_discount – discount.”,
Thanks for any insight you can give.
Hi Christopher,
Can you please try this updated version of the snippet and see if it helps?
https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-calc-subtotal.php
Best,
I am using the form to collect total entries of Random Acts of Kindness when someone enters the total “number” of total acts of kindness it charts it on the Ultimeter thermometer as total numbers of entries, not the actual number . Example, If someone is reporting 10 acts of kindness performed for a group, the Ultimeter only sees it as 1 but I need it to see it as the number entered, in this case, 10. Can you help ? https://localkindness.org
Hi Mary,
Thanks for writing in. From what I understand this should be possible with Gravity Forms eCommerce Fields perk. Another option would be using this other snippet: https://gravitywiz.com/gravity-forms-progress-meter/.
Best,
Hi,
i’ve this issue : Notice: Indirect modification of overloaded element of GF_Field_Checkbox has no effect in E:\Dropbox\SITES\test\www\wp-content\themes\test\inc\gw-calc-subtotal.php on line 123
Do you have an idea ?
Thank
Hi jlvz,
We’ll have to dig into this on your website, to determine why it isn’t working. If you have an active Gravity Perks License, you can get in touch with us via our support form so we can assist you. That said, you may want to use our GP Ecommerce Fields Perk, which is more user-friendly and up to date.
Best,
Thanks for this,
i used a multisite form with different products + options and amounts and need the following calculation :
Form page 01 with Product #1: Internet Flatrate (yes/no) + 25$ per month if yes –> show field »Private IP4 Adress (yes/no)« + 5$ per month if yes –> show field »Amount« (1-10) Subtotal = Price per Month (product01 + (option*amount))
Form page 02 with Product #2: Voice-over-IP (yes/no) + 15$ per month} if yes –> show field »Additional phone numbers (yes/no)« + 5$ per month} if yes –> show field »Amount« (1-10)} Subtotal = Price per Month (product 01 + (product 02 + (option*amount)))
Here is a small example: Internet Flat 25$ + 3xIP4(5$ per IP) – 40$ per month
Total: 95$ per month
Any ideas how I can achieve this?
Thanks for your help.
Hey Robby, To ensure that all calculations go through properly, your best bet may be the dedicated Gravity Forms eCommerce Fields perk we offer. All of the calculations can be handled with Gravity Forms Product plus options. Then to ensure the correct fields show you can use conditional logic. IF you have a Gravity Perks License we would be more than happy to take a look at the form. Just drop us a line through our support page. 😃
Thank you for this! It’s really helped.
I get a strange error though when I view an entry . Cannot modify header information – headers already sent by (output started at /home/****/public_html/****/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()’d code:222) in /home/*****/public_html/*****/wp-admin/admin-header.php on line 9
It’s coming from this portion: function add_merge_tags( $form ) {
<
script>
it only shows when I view an entry. I’ve checked for extra lines and deleted the space after the ?> but it’s still there.
It’s not affecting the site at all – just wondering if I should be looking into it further?
Hello Alissa, this is a bit of a strange one. Have you tried running a conflict test or ran some Snippet Troubleshooting to make sure nothing else was interfering with the code? IF you have a Gravity Wiz license we could take a closer look into this for you. You could reach out to us through our support page found here.
Does this plugin include calculating flat rate tax, for total purchase on the site?
Hello Anthony, This snippet would allow for the Subtotal Merge tag to be displayed and used within calculations. So you could set up a calculation field that uses the subtotal and calculates how much the tax should be that can later be used within the total, you can then name this tax to display how much they are being charged. Our Gravity Forms eCommerce Fields plugin already has a tax field pre-built within it to make it easier for you.
Thank you so much! This is epic for long forms. I wasn’t able to exclude tax from a “tip” field, but that’s okay! If anyone has a hint for me there, it’s appreciated. Thanks again for this solution!
Hello Sarah, how are you calculating the tax field currently? Let us know. 😃
Hi and thanks! A product field called “tax” (quantity field disabled) with the calculation: {subtotal} * .082
Here’s the working form in case it helps anyone else, too. It’s as simple as can be, for a temporary solution. They are a local food truck trying to keep some business going during the pandemic. foodlishus.com/order/
Hello Sarah, Sadly the subtotal field will take in all products. The only way around this would be to get the Gravity Forms eCommerce Fields perk. This will allow you to add a tax field as well as exclude products from the tax field.
Hello!
When I edit the single form entry in the admin side all the fields where the subtotal is used turn into zero values. Seems to be a bug or am I missing something here?
Thanks!
Hello Suusa, how are you currently using the subtotal field? Is it calculating numbers, products or what? Do you have a public site where we can see this information? Thank you so much!
Thanks for the fast replay. Yes, I’m using number field to calculate tax and show total on some products. Can’t give url at the moment but heres a screen: http://tiny.cc/5ukymz
Hello Suusa, I have just tested this out and everything seems to be working correctly. Are you using products to calculate the subtotal? When used with products the subtotal should calculate. Could you give that a try? 😀
Thanks for this,
trying to achieve the following:
Calculation: {subtotal} *0.15. (Add tax to product price). – WORKS PERFECT
Calculation 2: {subtotal} *0.025 (Add credit card fee). – HAVING PROBLEMS
TOTAL PRICE:
Problem is ‘calculation 2’ changes the subtotal which then makes it incorrect for calculation 1.
Any ideas how I can achieve this?
Thanks for your help.
Hey Michael, Looks like you are running into an issue with using two subtotal merge tags. Currently what is happening is the first calculation is running then replacing the subtotal with the new calculated subtotal. When you get to the next calculation it will now use the new subtotal in this calculation. You can accomplish this the easy way by getting Gravity Forms eCommerce Fields or you could actually reserve a subtotal field with just the {subtotal} merge tag, then use that fields price to create a new field with calculations that uses the new subtotal fields price merge tag ({Subtotal (Price):3.2}) to calculate the tax and fee. If you are still having issues after using the modifier this way, could you please drop us a support request. :)
I’m not having much luck getting this to work. I have a multi-page form with products on a few pages but the total for my calculation field is zero.
never mind. It was a formula error. Thanks for always being so awesome. Love you’re site and your code snippets.
Hello Dave,
Happy that you were able to get the calculation on your total form to work. Have a great day! :)
Can I able to use IF ELSE condition in merge tag calculation .please help me how we can do this. I am doing like below but it is not working for me. if($ 1-Year:90:48.88) 58.56 else 4.88
Gravity Forms doesn’t support if/else conditions in merge tags, but it does support conditional logic for number field. You could add conditional logic to a Product field and show/hide it based on the results of a calculation in a hidden number field.
You are great person, Thank you so much
❤️