Enable Decimal Values in Quantity Fields

October 17, 2022: Added support for fields using the decimal comma format.

September 24, 2019: Added support for Gravity Flow.

Gravity Forms has some pretty comprehensive support for various pricing fields but one thing it does not provide is a weight field. So what do you do when you’re selling something by the pound?

This snippet will allow you to accept decimal values in Quantity fields; converting any Quantity field into a weight field!

How do I install this snippet?

Easy peasy. Just paste it into your theme’s functions.php file.

Do I need to modify the snippet to work with my form?

Just a wee bit. You see the line at the very bottom of the snippet that looks like this:

new GW_Quantity_Decimal( 34, array( 1, 3 ) );

The first parameter is the $form_id for which this functionality should be enabled. The second parameter is an array of field IDs for which this functionality should be enabled.

If you want to enable this functionality on all product and quantity fields for a specific form, you would just do something like this (replacing the 34 with the ID of your form):

new GW_Quantity_Decimal( 34 );

If you want to enable this functionality for a single field on your form, initialize the class like this:

new GW_Quantity_Decimal( 34, 1 );

Summary

Now that your quantities can support decimal values, what else do you want to learn how to do with Gravity’s WordPress forms?

Comments

  1. Mike Frising
    Mike Frising October 27, 2023 at 9:00 am

    I have tried installing the plugin as well as putting it into the functions.php file and it is still not allowing decimal points. I have configured new GW_Quantity_Decimal( 34 ); to the correct form ID number and nothing is working for me. Any help would be appreciated. Thank you

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff October 27, 2023 at 9:29 am

      Hi Mike,

      I just tested the snippet multiple times and it works as expected. Here’s a screenshot of the order summary after submission. As you will see, the quantity is in decimal. If it’s still not working for you, we’ll need you to submit a ticket, so we can look at the setup on your end, and assist you to get it working.

      Best,

    2. Mike Frising
      Mike Frising November 1, 2023 at 9:53 am

      OK I got it to work. However, you need to add an integer first. For example I can’t use .5 I have to use 0.5. Do you know if there’s a way around that? And I would set up a ticket but where do I register for an account?

      Thanks for your help.

  2. Rene
    Rene July 8, 2022 at 4:20 am

    Hi, I installed the plugin, and copied the code to the functions file. Looks like it is not working?

    I used new GW_Quantity_Decimal( 1 ); in the php file. Where I am using 1 form and I think it has ID 1?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff July 8, 2022 at 6:16 am

      Hi Rene,

      Yes, the 1 should be the Form ID. You can confirm the Form ID on the Forms page. I just tested the code snippet and it still works. We’ll have to look into this on your end to identify why it isn’t working for you. If you have an active Gravity Perks license, you can get in touch with us via our support form so we can assist you further.

      Best,

  3. Roy Claassen
    Roy Claassen April 23, 2021 at 8:18 am

    Works fine! Is there a quick way to allow . and , for same decimals? At this moment when I use for example 1.2 the outcome of a field with simple calculation is the way it should. It uses the decimals. Using the , in the same (quantity) input field, the result is without the decimal.

    Example: input of quantity field is 1.2 [ the formula/product price field = {Aantal m1:2}*2 ] the outcome will be 2.4

    When the input of quantity field is 1,2 [ the formula/product price field = {Aantal m1:2}*2 ] the outcome will be 24 (it should be the same result -> 2.4)

    Number notation is set to 9,999.99 (if not, like someone else replied, the snippet doesn’t work at all)

    Reply
    1. Dario Space
      Dario Space April 23, 2021 at 8:36 am

      Hi Roy,

      Try replacing the instances of “decimal_dot” in the snippet to “decimal_comma”. Depending on the currency settings you have set on GF you could need additional tweaks.

  4. Pierre
    Pierre July 2, 2020 at 6:58 am

    Hello,

    Our website uses decimal points on all of its products to calculate the price based on length. Is there a way to enable this for all forms without having to specify each ID? We have dozens of forms, different people managing the shop and only one person able to edit the code, so it would not be manageable to only be able to apply this snippet on specific forms, one at a time.

    Thanks

    Reply
    1. Ryan Donovan
      Ryan Donovan July 28, 2020 at 12:39 pm

      Hello Pierre, we have reached out via our support channel for this one. Have a great day. 😃

  5. Quantity - Gravity Forms Documentation September 24, 2018 at 6:46 am

    […] Allowing the user to type decimal values for the quantity is not supported. Said that, there’s a third-party script that you can use to override this limitation: Enable Decimal Values in Quantity Fields […]

    Reply
  6. Michael Muhonen
    Michael Muhonen August 18, 2017 at 4:19 am

    This snippet is still not working. Yes, it accepts either dot or comma but doesn’t deliver the decimal form to WooCommerce order. For instance, it may show 0.5kg of order as plain 0. Then the online store owner needs to quess how much the client had ordered. I guess this is not how this should work? The prices are calculated correctly though.

    The online store is using Gravity Forms and products addon (woocommerce).

    Reply
    1. David Smith
      David Smith Staff August 18, 2017 at 7:23 am

      Hi Michael, it sounds like there is a conflict with how the WC GF Product Add-ons plugin is processing the form. We don’t guarantee support for 3rd-party add-ons but if you’re already a Gravity Perks customer, submit a support request and we’ll be happy to take a closer look.

  7. Chris
    Chris June 3, 2016 at 11:05 am

    I tried this solution, but nothing changed, I am still getting a validation error when decimals are entered. Do you have any tips for troubleshooting this snippet?

    Reply
    1. Chris
      Chris June 3, 2016 at 11:05 am

      Sorry, for some reason I didn’t get a notification of your last reply. You can delete this comment.

    2. David Smith
      David Smith Staff June 3, 2016 at 12:03 pm

      Hi Chris, we do offer a $99 installation service. Ping me at david [at] gravitywiz [dot] com if you’re interested. :)

    1. Chris
      Chris June 3, 2016 at 11:07 am

      I’ll looked at those tips but I’m not getting an error, the snippet just isn’t working. Are you available to troubleshoot this further with me?

  8. Rose
    Rose November 23, 2015 at 9:14 pm

    Hi there!

    I used this code on my site but when I did, it didn’t allow the paypal to go through or submit. The form accepts the decimal fine, but now it’s not accepting payment.

    Thanks so much!

    Reply
    1. David Smith
      David Smith Staff November 25, 2015 at 8:35 am

      It looks like you’re correct. There must have been a change in the PayPal add-on. I will fix this in a future version of the snippet.

    1. David Smith
      David Smith Staff October 26, 2015 at 9:34 am

      It’s not possible to use this with Number fields currently; however, could you use a Quantity field instead of a Number field and it would work much the same way.

    1. David Smith
      David Smith Staff September 7, 2015 at 10:18 pm

      Try replacing the instances of “decimal_dot” in the snippet to “decimal_comma”. Not sure if it will work but worth a shot. :)

  9. Pete
    Pete February 13, 2015 at 8:08 am

    I too cant get this snippet to work. Iv’e tried periodically over the last year always updating wordpress & gravity forms. I’ve resorted to using number fields to get it accomplished but i’d much prefer this way. i have 1.9 gravity forms and 4.1 wp installed

    Reply
    1. David Smith
      David Smith Staff February 13, 2015 at 6:50 pm

      Hey Pete, sent you an email. I’d like to see if we can’t figure out what’s going wrong. This is working for me locally and on the demo.

  10. Long
    Long November 23, 2014 at 6:04 pm

    I paste the code into function.php and got this error.

    Parse error: syntax error, unexpected ‘<‘ in /home/doteksol/public_html/wp-content/themes/envision/functions.php on line 35

    how to fix it?

    Reply
  11. karl
    karl October 6, 2014 at 5:03 am

    I have tried adding the code an it all seems to be in the right place, but i am still getting the error message saying that it wont accept the decimal in the form

    any ideas?

    cheers

    Reply
  12. Philipp
    Philipp June 20, 2014 at 7:19 am

    Hi, is still supposed to work? I get the “Please enter a valid value” error message when typing something like 1.2 into the quantity field. I tried both, the global and the field specific solution. Thanks, Philipp

    Reply
    1. David Smith
      David Smith Staff June 20, 2014 at 8:40 am

      Hi Philipp, for single product quantity fields (where the quantity field is inline with the price), you do not need to specify the input ID. The snippet is smart enough to determine the correct quantity based on the field ID of the product field itself. Here’s a demo and here is how I’ve initialized the demo:

      new GW_Quantity_Decimal( 34, array( 1, 3 ) );

    2. Philipp
      Philipp June 23, 2014 at 11:41 am

      Strange, it doesn’t work for me. I double checked that I have the right form ID and field ID (also tried without assigning any field ID)…

      The rest of my functions.php is working fine – can’t be that I’m in the wrong place or something…

      I don’t need your Gravity Perks for it to work, do I?

    3. David Smith
      David Smith Staff June 23, 2014 at 8:07 pm

      Sent you an email, Philipp. If you can provide me some access, I’ll take a look and see what might be going wrong.

    4. Philipp
      Philipp June 30, 2014 at 11:56 am

      Figured it out in the end: it has something to do with HTML5 and how Bootstrap (that’s where my theme is based on) do the validation for the input field. That was causing the error message in the first place.

      I resolved it by adding step="any" to the input field. As there’s no direct access to that code, I used jQuery for adding it: $(".ginput_quantity").attr('step', 'any'); (you might want to use a different selector if you don’t want to change all quantity fields)

      Thanks for the kind support!

  13. Jorge
    Jorge August 25, 2012 at 6:27 am

    Thanks for that, but i’m having a few problems as it is accepting decimal values for ALL fields:

    My code: http://pastebin.com/dY1Dj5r2

    Reply
    1. David Smith
      David Smith Staff August 27, 2012 at 4:30 pm

      The snippet has been updated to allow you to specify which form and which fields on that form this should be applied to.

    1. David Smith
      David Smith Staff August 22, 2012 at 10:05 am

      It is possible. This snippet currently adds “weight” support globally; however, you could limit it to a specific form by changing the filter name from “gform_validation” to “gform_validation_1” (the “1” being the ID of your form). If you wanted to only allow “weight” support to a specific field on a specific form, you could add the following below the foreach loop:

      if($field['id'] != 1) { if($field['failed_validation']) $has_validation_error = true; continue; }

      Replace the “1” with the ID of your field.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

Enable Decimal Values in Quantity Fields

This field is for validation purposes and should be left unchanged.