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.


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]
Fixed!
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/
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?
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.
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?
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.
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?
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.
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.
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.
“Download Plugin” not working?
Fixed. ?
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!
Hm, I’m not sure I understand? Is this caused by the {subtotal} merge tag?
My apologies. I needed to upgrade Stripe Add-On from 2.5 to 2.5.4. That fixed my issue. Thank you for responding.
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.
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/
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!
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.
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?
Hi Scott, I’ve added a proper plugin header to the snippet.
Thanks, David!
THANK YOU!!!
My pleasure, Jocelyn. ?
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
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
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!
Hello all, this has been fixed. Grab the latest version of the snippet above.
Thank you David! confirmed working. I appreciate it, have a nice weekend.
Thanks very much David for the speedy update. Much appreciated!
Glad to help! ?
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.
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.
I believe that this snippet stopped working with the release of the new 2.4 version.
Fixed. Latest version above. :)
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.
Disregard my last comment. sorry i figured it out!
Thanks for this. I used this solution to allow me to use the subtotal in a conditional statement for another field. Very helpful.
Glad it helped you!
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
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?
Hi Ronny, we don’t have a solution for this one.
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!
Hard to say, Janice. If you’re a Gravity Perks customer, we’ll be happy to dig in via the support form.
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?
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.
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.
Add a negative sign in front of of your formula.
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.
Aha!
I added another set of parenthesis around the whole thing and now it’s calculating properly!