• 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 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. Christopher Grasso says

    November 8, 2022 at 3:05 am

    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.

    Reply
    • Samuel Bassah says

      November 8, 2022 at 7:14 am

      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,

  2. Mary Stewart says

    January 19, 2022 at 12:00 pm

    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

    Reply
    • Dario Space says

      January 19, 2022 at 12:24 pm

      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,

  3. jlvz says

    November 10, 2021 at 2:05 am

    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

    Reply
    • Samuel Bassah says

      November 10, 2021 at 3:13 am

      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,

  4. Robby says

    May 27, 2020 at 8:21 am

    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

    • VoIP 15$ + 6*additional number(5$ per number) = 45 $ per month

    Total: 95$ per month

    Any ideas how I can achieve this?

    Thanks for your help.

    Reply
    • Ryan Donovan says

      May 27, 2020 at 11:21 am

      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. 😃

  5. Alissa Eggleston says

    May 22, 2020 at 7:51 am

    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 ) {

        $label = __('Subtotal', 'gravityforms');
    
        ?>
    

    <

    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?

    Reply
    • Ryan Donovan says

      May 22, 2020 at 10:51 am

      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.

  6. Anthony says

    May 20, 2020 at 2:24 pm

    Does this plugin include calculating flat rate tax, for total purchase on the site?

    Reply
    • Ryan Donovan says

      May 20, 2020 at 2:43 pm

      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.

  7. Sarah says

    April 17, 2020 at 11:20 am

    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!

    Reply
    • Ryan Donovan says

      April 17, 2020 at 11:55 am

      Hello Sarah, how are you calculating the tax field currently? Let us know. 😃

    • Sarah says

      April 17, 2020 at 1:41 pm

      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/

    • Ryan Donovan says

      April 17, 2020 at 2:04 pm

      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.

  8. Suusa says

    April 13, 2020 at 9:24 am

    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!

    Reply
    • Ryan Donovan says

      April 13, 2020 at 10:03 am

      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!

    • Suusa says

      April 13, 2020 at 12:51 pm

      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

    • Ryan Donovan says

      April 13, 2020 at 1:07 pm

      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? 😀

  9. Michael says

    February 25, 2020 at 12:28 am

    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.

    Reply
    • Ryan Donovan says

      February 25, 2020 at 10:20 am

      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. :)

  10. Dave says

    January 27, 2020 at 12:24 pm

    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.

    Reply
    • dave says

      January 27, 2020 at 12:40 pm

      never mind. It was a formula error. Thanks for always being so awesome. Love you’re site and your code snippets.

    • Ryan Donovan says

      February 14, 2020 at 12:10 pm

      Hello Dave,

      Happy that you were able to get the calculation on your total form to work. Have a great day! :)

  11. shikha choudhary says

    January 23, 2020 at 2:55 am

    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

    Reply
    • Scott Buchmann says

      February 17, 2020 at 2:59 pm

      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.

  12. rafiqul Islam says

    January 7, 2020 at 1:01 am

    You are great person, Thank you so much

    Reply
    • David Smith says

      January 7, 2020 at 3:38 pm

      ❤️

« Older Comments

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

  • How to Create Timesheets with Gravity Forms
  • Spotlight: Auto Classmate’s AI Powered Teaching Tools
  • Introducing Gravity Forms OpenAI Beta 1 featuring GPT 3.5 Turbo
  • Spotlight: building an internal payroll tool using Gravity Forms Entry Blocks
  • Our 46th perk is here. Say hello to Advanced Save & Continue.

Categories

  • How To (79)
  • News (25)
  • Plugins (21)
  • Releases (20)
  • Resource (4)
  • Snippets (54)
  • Spotlights (20)
  • Tutorials (58)
  • Updates (182)

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