Let’s say you sell business cards. Orders for these business cards must be placed in increments of 500. If you try to order 499 cards, you’re going to get 500. If you try to order 600, you’re going to get 1000 (the next increment of 500). Do you see how this works?
This snippet provides a way to round field values (particularly for Number, Quantity and Calculation fields) up to the nearest specified increment. In addition, it provides a way to cap the increment and set a minimum.
How do I install this snippet?
- You will want to be running at least Gravity Forms v.1.8 though it may work with some older versions.
- Already have a license? Download Latest Gravity Forms
- Need a license? Buy Gravity Forms
- Copy and paste the snippet into your theme’s functions.php file.
- This snippet requires no other configuration in the code. Continue reading for usage instructions.
Usage Instructions
This snippet is powered by special CSS classes which can be applied to a field via the “CSS Class Name” setting. Here are step by step instructions:
- Open your form for editing.
- Open the fielding settings for the field you would like to round incrementally.
- Navigate to the “Advanced” tab.
- Select the “CSS Class Name” setting and add the “gw-rounding-n” class, replacing n with the increment to which you would like this field to round.
All Options
gw-round-n | round to the nearest increment; will round up or down to the nearest increment example: gw-round-100 would round 1 to 100, 149 to 100, 150 to 200, etc |
gw-round-up-n | round up to the nearest increment; will always round up example: gw-round-up-50 would round 1 to 50, 51 to 100, 149 to 150, etc |
gw-round-down-n | round down to the nearest increment; will always round down example: gw-round-down-25 would round 25 would round 1 to 0, 26 to 25, 51 to 50, etc |
gw-round-min-n | round up to the specified minimum if not reached example: gw-round-min-50 would round 1 to 50, 49 to 50, 51 and greater would not be rounded |
gw-round-max-n | round down to the specified maximum if exceeded example: gw-round-max-250 would round 251 to 250, 400 to 250, 250 and below would not be rounded |
What if I’m already using the “CSS Class Name” setting for another CSS class?
No problem. The “CSS Class Name” setting supports multiple classes and that won’t interfere with the special “gw-round-n” class. Just add a space after the last class and add the “gw-round-n” class after it.
In closing
You know the drill. If you like it, use it or improve it, let us know!