Closed tedraykov closed 1 year ago
When a cart that has an applied discount is updated, e.g. an item is added, the discounts are not correctly calculated.
Example:
2
The cart undergoes all the transformations before being saved and one of the transformations is the setDiscountsOnCart. The discounts plugin calls all registered discount codes functions for every discount on the cart: https://github.com/reactioncommerce/api-plugin-discounts/blob/9d1bc40f86cea520f234b3c812c117400c795998/src/queries/getDiscountsTotalForCart.js#L26
setDiscountsOnCart
The problem is that at this stage the new cart is not yet persisted as not all transformations had finished so in the discount code function we are fetching the last persisted cart from the database, rather than using the cart that is currently being transformed and saved: https://github.com/reactioncommerce/api-plugin-discounts-codes/blob/643c7f7e6594f9539037804a8055e0a531540a84/src/util/getPercentageOffDiscount.js#L23
A solution is to use the cart that is passed to the discount transformation function rather than fetching the cart from the database.
This change would require a change in the discount functions parameters both in the api-plugin-discounts and in api-plugin-discounts-codes
api-plugin-discounts
api-plugin-discounts-codes
Closing this issue since the existing coupon code implementation is being deprecated in release 5 and replaced with a new implementation.
When a cart that has an applied discount is updated, e.g. an item is added, the discounts are not correctly calculated.
Example:
2
The cart undergoes all the transformations before being saved and one of the transformations is the
setDiscountsOnCart
. The discounts plugin calls all registered discount codes functions for every discount on the cart: https://github.com/reactioncommerce/api-plugin-discounts/blob/9d1bc40f86cea520f234b3c812c117400c795998/src/queries/getDiscountsTotalForCart.js#L26The problem is that at this stage the new cart is not yet persisted as not all transformations had finished so in the discount code function we are fetching the last persisted cart from the database, rather than using the cart that is currently being transformed and saved: https://github.com/reactioncommerce/api-plugin-discounts-codes/blob/643c7f7e6594f9539037804a8055e0a531540a84/src/util/getPercentageOffDiscount.js#L23
A solution is to use the cart that is passed to the discount transformation function rather than fetching the cart from the database.
Breaking change
This change would require a change in the discount functions parameters both in the
api-plugin-discounts
and inapi-plugin-discounts-codes