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

[Issue] Sales rule CartFixed calculation : incorrect discount amount #38694

Open
1 of 5 tasks
Nuranto opened this issue May 2, 2024 · 15 comments
Open
1 of 5 tasks

[Issue] Sales rule CartFixed calculation : incorrect discount amount #38694

Nuranto opened this issue May 2, 2024 · 15 comments
Labels
Area: Cart & Checkout Component: Ui Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Progress: ready for dev Reported on 2.4.7 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch

Comments

@Nuranto
Copy link
Contributor

Nuranto commented May 2, 2024

Preconditions and environment

  • Magento version 2.4.7

Steps to reproduce

  1. Create a sales rule "test1", without coupon, cart fixed amount = 5€ (Discard subsequent rules = no)
  2. Create a sales rule "test2", without coupon, cart fixed amount = 10€ (Discard subsequent rules = no)
  3. Create a sales rule "test3", with coupon 'test3', cart fixed amount = 1€ (Discard subsequent rules = no)
  4. Create a frontend cart, with total amount > 20€
  5. Add coupon test3

Expected result

Discount of 16€

Actual result

Discount different of 16€, amount change when cart items changes.

Additional information

The difference between amount applied and correct amount can be very big - it depends on rules amounts, and items in cart. In one of our cases, we saw a 90€ discount applied, instead of 40€...

I'm still on debug. The issue is probably somewhere between src/vendor/magento/module-sales-rule/Model/Rule/Action/Discount/CartFixed.php::calculate and src/vendor/magento/module-sales-rule/Helper/CartFixedDiscount.php::getDiscountedAmountProportionally. I will update the issue if I get more informations.

Update : I really don't understand what's the goal of getDiscountedAmountProportionally method. It can sometimes - and sometimes it does - produce a ratio bigger than 1, hence appliying a bigger discount than expected. Even when the ratio is under 1, I don't really get why we lower the discount. I guess such "proportionality" could be usefull for % discounts, to avoid that two -10% sum up at -20% instead of -19% (-10% on a -10% discount). But for fixed cart price, I don't understand..

So a quickfix is to simply remove it, by replacing if ($isAppliedToShipping) { with if (true || $isAppliedToShipping) { in CartFixed class. This is of course a temporary ugly fix, that you can apply at your own risk, but in our case it fixed the error and we stopped losing money (or making our customers angry) because of it. Now, let's wait for an answer from a Magento architect.

Update 2 : Same issue with rules that only applies on shipping amounts. (apply on shipping amount = Yes + Apply on product where name = 'some inexistant name').
Seems because of another ratio in src/vendor/magento/module-sales-rule/Helper/CartFixedDiscount.php::getShippingDiscountAmount. But #38671 needs to be fixed to be tested.

Quick fix for that (at your own risks) :

--- a/Model/Validator.php	2024-05-03 08:38:15
+++ b/Model/Validator.php	2024-05-03 08:38:05
@@ -486,6 +486,14 @@
                                     $quoteBaseSubtotal,
                                     $shippingQuoteAmount
                                 );
+
+                            $quoteBaseSubtotal = (float) $address->getBaseShippingAmount();
+                            foreach ($quote->getAllItems() as $item) {
+                                if ($rule->getActions()->validate($item)) {
+                                    $quoteBaseSubtotal += (float) $item->getBaseRowTotal();
+                                }
+                            }
+
                             $discountAmount = $this->cartFixedDiscountHelper->
                             getShippingDiscountAmount(
                                 $rule,

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
Copy link

m2-assistant bot commented May 2, 2024

Hi @Nuranto. Thank you for your report.
To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel.
⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
🕙 You can find the schedule on the Magento Community Calendar page.
📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

Copy link

m2-assistant bot commented May 2, 2024

Hi @engcom-Dash. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

    1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    1. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
    1. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
    1. Verify that the issue is reproducible on 2.4-develop branch
      Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
      - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
      - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

@engcom-Delta engcom-Delta self-assigned this May 2, 2024
Copy link

m2-assistant bot commented May 2, 2024

Hi @engcom-Delta. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • 5. Add label Issue: Confirmed once verification is complete.
  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Delta engcom-Delta added the Reported on 2.4.7 Indicates original Magento version for the Issue report. label May 2, 2024
@Nuranto Nuranto changed the title [Issue] Sales rule calculation : incorrect discount amount [Issue] Sales rule CartFixed calculation : incorrect discount amount May 2, 2024
@engcom-Dash
Copy link

@magento give me 2.4-develop instance

Copy link

Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.

Copy link

@engcom-Dash
Copy link

@magento give me 2.4-develop instance

Copy link

Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.

Copy link

@engcom-Dash
Copy link

Hi @Nuranto

Thanks for reporting and collaboration.

Verified the issue on magento 2.4 dev instance and 2.4.7 instance and the issue is reproducible.

We are seeing a Discount difference of 16€, the amount changes when cart items change.

Steps to reproduce

  1. Create a sales rule "test1", without coupon, cart fixed amount = 5€ (Discard subsequent rules = no)
  2. Create a sales rule "test2", without coupon, cart fixed amount = 10€ (Discard subsequent rules = no)
  3. Create a sales rule "test3", with coupon 'test3', cart fixed amount = 1€ (Discard subsequent rules = no)
  4. Create a frontend cart, with total amount > 20€
  5. Add coupon test3

Please refer the attached screenshots.

Discount amount after applying coupon code test3:

Screenshot 2024-05-06 at 10 45 55 PM

When cart amount/products are changed :

Screenshot 2024-05-06 at 10 46 25 PM

Cart rules Configuration:

Screenshot 2024-05-06 at 10 53 26 PM Screenshot 2024-05-06 at 10 53 36 PM Screenshot 2024-05-06 at 10 53 45 PM Screenshot 2024-05-06 at 11 12 54 PM

@engcom-Dash engcom-Dash added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Component: Ui Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Area: Cart & Checkout labels May 7, 2024
@m2-community-project m2-community-project bot moved this from Ready for Confirmation to Confirmed in Issue Confirmation and Triage Board May 7, 2024
@github-jira-sync-bot
Copy link

✅ Jira issue https://jira.corp.adobe.com/browse/AC-11914 is successfully created for this GitHub issue.

Copy link

m2-assistant bot commented May 7, 2024

✅ Confirmed by @engcom-Dash. Thank you for verifying the issue.
Issue Available: @engcom-Dash, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@engcom-Hotel engcom-Hotel added the Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. label May 7, 2024
@m2-community-project m2-community-project bot added this to Ready for Development in High Priority Backlog May 7, 2024
@zakdma
Copy link
Contributor

zakdma commented May 9, 2024

I don't know the exact place where the issue appeared but I found that the cart totals calculation was called twice.
Also I found that in M2.4.6-p4 all worked fine and totals calculation called only once.

@zakdma
Copy link
Contributor

zakdma commented May 9, 2024

Looks like these commits introduced break changes
4ffcb02
c741823

@pmzandbergen
Copy link
Contributor

I can confirm this issue. We ran into this while running functional tests with the Magento 2.4.7 upgrade. Previous version (2.6.4-p4) did succeed the same tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Cart & Checkout Component: Ui Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Progress: ready for dev Reported on 2.4.7 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch
Projects
High Priority Backlog
  
Ready for Development
Development

No branches or pull requests

7 participants