snippet-library
snippet-library copied to clipboard
`gw-gravity-forms-rounding.php`: Fixed an issue with rounding now updating form total.
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2981539269/85760
Summary
The GF Rounding snippet isn't working as expected.
ISSUE 1
The minimum rounding doesn't work when the value is entered. It works only when you're using the number field controls to increase or decrease the value.
We had if( value != '' ), which we changed to if( value !== '' )
Switching from loose inequality (!=) to strict inequality (!==) to prevent unintended type coercion. This ensures that numeric 0 and empty string '' are treated as distinct values during comparison. With !=, javascript converts 0 != '' as a numeric comparison with both values as 0.
ISSUE 2
When the rounding is applied to a quantity value, it doesn't update the total.
Using $input.val( value ).change(); alone wasn’t enough to trigger Gravity Forms' total recalculation in some cases. Gravity Forms listens for native input events on fields like quantity and product fields for live calculations. We add a native input event dispatch after setting the value, ensuring Gravity Forms detects the change and recalculates totals as if the user had typed the value manually.
BEFORE (Samuel's loom): https://www.loom.com/share/c94f0966f80f495db537dc68e7d5da81
AFTER: https://www.loom.com/share/b012dd305bdb4ed1b800c2ef3957bcf9