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?
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
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,
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.
This will require some digging from our developers, Mike. You will need to have active a license to open a support request.
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?
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,
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)
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.
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
Hello Pierre, we have reached out via our support channel for this one. Have a great day. 😃
[…] 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 […]
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).
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.
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?
Sorry, for some reason I didn’t get a notification of your last reply. You can delete this comment.
Hi Chris, we do offer a $99 installation service. Ping me at david [at] gravitywiz [dot] com if you’re interested. :)
I tried this snippet by I am still getting a validation error saying it won’t accept decimals. I am using the Stripe addon for Gravity Forms. Does this snippet depend on gravity perks?
Here’s the url: https://mythcreants.com/services/order-a-consultation/
Thank you!
Hi Chris, this snippet does not require Gravity Perks. Try these troubleshooting steps as a start: https://gravitywiz.com/documentation/snippet-troubleshooting/
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?
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!
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.
Thanks for this! It is great. How to use for number fields?
Best, Rosi
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.
Would it be possible to adapt this script for the European way of formatting decimals, “9.999,99”?
It works for me, but just when using the “9,999.99” format
http://www.haugengardsmat.no/skjematest/
Thanks in advance
Try replacing the instances of “decimal_dot” in the snippet to “decimal_comma”. Not sure if it will work but worth a shot. :)
I also did not have any luck with the code allowing for decimals.I am using WordPress 4.1 and Gravity Forms 1.9.1.2
Hi Marcy, try these installation tips: https://gravitywiz.com/documentation/snippet-troubleshooting/
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
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.
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?
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
Hi Karl, sent you an email requesting more details.
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
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 ) );
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?
Sent you an email, Philipp. If you can provide me some access, I’ll take a look and see what might be going wrong.
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!
Aweseome! Glad you were able to find a solution. :)
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
The snippet has been updated to allow you to specify which form and which fields on that form this should be applied to.
Hi again.
Do you think it is possible to allow some items on the form to be purchased by weight, and some by qty?
Also, I have some code which changes the label from qty to weight for those of you who wish to do this…
http://pastebin.com/LYQbXzBb
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.