Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Rounding Issue in Cart/Orders #5091

Merged
merged 2 commits into from
Apr 3, 2019
Merged

Conversation

nnnnat
Copy link
Contributor

@nnnnat nnnnat commented Apr 2, 2019

Impact: critica
Type: bugfix

Issue

When using Reaction with 3rd party pricing engines Reaction will sometimes incorrectly round the carts total/subtotal. For example adding 6 product with a price of $2.99 would result in a cart/order total of $17.93999999999994.

Solution

In this PR I've wrapped any cart/order "money math" with accounting-js toFixed function to force the correct rounding.

Breaking changes

N/A

Testing

  1. Do the order creation script to test these changes. Make sure you add multiples of the same product to your cart. We want to verify the price * quantity calculations are also working.

@nnnnat nnnnat force-pushed the fix-nnnnat-total-rounding branch from b1f3cc3 to 80bfa92 Compare April 2, 2019 23:10
… against a price value is rounded correctly.

Signed-off-by: Nat Hamilton <[email protected]>
… against a price value rounds correctly.

Signed-off-by: Nat Hamilton <[email protected]>
@@ -133,7 +134,7 @@ export default async function addCartItems(context, currentItems, inputItems, op
shopId: catalogProduct.shopId,
// Subtotal will be kept updated by event handler watching for catalog changes.
subtotal: {
amount: variantPriceInfo.price * quantity,
amount: +toFixed(variantPriceInfo.price * quantity, 3),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I reading this right that we're fixing to 3 decimal places? If so, why 3?

Copy link
Member

@kieckhafer kieckhafer Apr 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based of @aldeed's previous work on related issues, some currency codes go up to 3 decimals, so we need to accommodate those.

Check the E column of the Active Codes table: https://en.wikipedia.org/wiki/ISO_4217. Those listed with 4 are special inter-country currencies, and don't apply.

Copy link
Contributor

@aldeed aldeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nnnnat If this needs to be merged ASAP, I'm fine with it, but I strongly suggest a follow-up PR to add a unit test for each updated util function, which shows an example of problematic math and proves these fixes work (fails when you changes are reverted, passes after). This is a type of bug we definitely want to have tests for so that it doesn't recur when code is refactored.

@aldeed
Copy link
Contributor

aldeed commented Apr 2, 2019

I want to also point out that while this should mitigate 99.999% of cases, it doesn't really FIX the issue of JS math errors. I've previously documented some of the gotchas here: #4692

@spencern It may be a good time to focus some effort on figuring out a 100% foolproof solution to storing and calculating with floats before this crops up again.

Copy link
Member

@kieckhafer kieckhafer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 products @ 69.99 each works. See @aldeed's above comment about a long term solution, and testing, but for a right now fix: 👍

@nnnnat nnnnat merged commit f090245 into develop Apr 3, 2019
@nnnnat nnnnat deleted the fix-nnnnat-total-rounding branch April 3, 2019 02:33
@jeffcorpuz jeffcorpuz mentioned this pull request Jul 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants