reactioncommerce / reaction

Mailchimp Open Commerce is an API-first, headless commerce platform built using Node.js, React, GraphQL. Deployed via Docker and Kubernetes.
https://mailchimp.com/developer/open-commerce/
GNU General Public License v3.0
12.34k stars 2.17k forks source link

Discount calculated based on old state of the cart #6481

Closed tedraykov closed 1 year ago

tedraykov commented 2 years ago

When a cart that has an applied discount is updated, e.g. an item is added, the discounts are not correctly calculated.

Example:

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

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.

Breaking change

This change would require a change in the discount functions parameters both in the api-plugin-discounts and in api-plugin-discounts-codes

brent-hoover commented 1 year ago

Closing this issue since the existing coupon code implementation is being deprecated in release 5 and replaced with a new implementation.