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 · 27 comments · May be fixed by #39231
Open
1 of 5 tasks

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

Nuranto opened this issue May 2, 2024 · 27 comments · May be fixed by #39231
Assignees
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: PR in progress Progress: ready for grooming 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
@github-jira-sync-bot
Copy link

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

@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.

@rutvik-dolphin
Copy link

rutvik-dolphin commented Jun 24, 2024

Without coupons, the discount is not being calculated correctly. I created four rules with a "Fixed amount discount for whole cart" and a "Discount Amount" of 10. I then tested different subtotals on the frontend, and here are the results:
Subtotal: 335.00, Discount: 41.42
Subtotal: 303.00, Discount: 41.61
Subtotal: 229.00, Discount: 42.28
Subtotal: 170.00, Discount: 40.76
Subtotal: 125.00, Discount: 41.51
Subtotal: 92.00, Discount: 43.70
Subtotal: 60.00, Discount: 46.76

The expected discount is 40, and it should be the same for all subtotals because the rule is set to "Fixed amount discount for whole cart". I checked the same scenario in 2.4.6-p4 and it is working as expected.

@dsengsqli
Copy link

Do someone have an hotfix because we are facing the issue on our live website and its a urgent major issue
The issue has been reported 2 months ago can we expect a patch soon ?

@pmzandbergen
Copy link
Contributor

Do someone have an hotfix because we are facing the issue on our live website and its a urgent major issue The issue has been reported 2 months ago can we expect a patch soon ?

Apply the following patches on the Magento SalesRule module, you can use https://github.com/cweagans/composer-patches for this.

--- Model/Validator.php.org	2024-05-27 09:47:36
+++ Model/Validator.php	2024-05-27 09:48:03
@@ -187,6 +187,11 @@
         $this->_rules = null;
     }
 
+    public function clearRules(): void
+    {
+        $this->_rules = [];
+    }
+
     /**
      * Init validator
      * Init process load collection of rules for specific website,
@@ -486,6 +491,16 @@
                                     $quoteBaseSubtotal,
                                     $shippingQuoteAmount
                                 );
+
+                            // START: Fix issue #38694
+                            $quoteBaseSubtotal = (float) $address->getBaseShippingAmount();
+                            foreach ($quote->getAllItems() as $item) {
+                                if ($rule->getActions()->validate($item)) {
+                                    $quoteBaseSubtotal += (float) $item->getBaseRowTotal();
+                                }
+                            }
+                            // END
+
                             $discountAmount = $this->cartFixedDiscountHelper->
                             getShippingDiscountAmount(
                                 $rule,

and

--- Model/Rule/Action/Discount/CartFixed.php.org	2024-05-27 09:40:39
+++ Model/Rule/Action/Discount/CartFixed.php	2024-05-27 09:40:47
@@ -117,7 +117,10 @@
                         $baseRuleTotals,
                         $shippingPrice
                     ) : $baseRuleTotals;
-            if ($isAppliedToShipping) {
+            // START: Fix issue #38694
+            // if ($isAppliedToShipping) {
+            if (true || $isAppliedToShipping) {
+            // END
                 $baseDiscountAmount = $this->cartFixedDiscountHelper
                     ->getDiscountAmount(
                         $ruleDiscount,

@dsengsqli
Copy link

Thanks !
I will install it as hotfix but i hope we will get an official fix soon.

@amcguireweb
Copy link

Thanks ! I will install it as hotfix but i hope we will get an official fix soon.

Did you get an official patch for this from Adobe Commerce support?

@joacols
Copy link

joacols commented Sep 30, 2024

@magento I am working on this

Copy link

m2-assistant bot commented Sep 30, 2024

Hi @joacols! 👋
Thank you for collaboration. Only members of Community Contributors Team are allowed to be assigned to the issue. Please use @magento add to contributors team command to join Contributors team.

@joacols
Copy link

joacols commented Sep 30, 2024

@magento add to contributors team

Copy link

m2-assistant bot commented Sep 30, 2024

Hi @joacols! 👋
Thank you for joining. Please accept team invitation 👉 here 👈 and add your comment one more time.

@joacols
Copy link

joacols commented Sep 30, 2024

@magento add to contributors team@magento I am working on this

joacols pushed a commit to joacols/magento2 that referenced this issue Oct 1, 2024
- Add init totals into foreach rules loop to allow for total calculation after each rules apply on Discount.php

- remove logic for adding item discount to address base discount on Discount.php

- on CartFixed.php now use validator to calculate current valid items total discount for rule on proporcional discount calculation.

refs: magento#38694
@joacols joacols linked a pull request Oct 1, 2024 that will close this issue
5 tasks
@m2-community-project m2-community-project bot assigned joacols and unassigned joacols Oct 1, 2024
@joacols
Copy link

joacols commented Oct 1, 2024

Hi,
My code for this is on this pr:
#39231

@m2-community-project m2-community-project bot assigned joacols and unassigned joacols Oct 1, 2024
@engcom-Bravo
Copy link
Contributor

Hi @Nuranto,

Thank you for your contribution!

The Magento core engineering team is working on the issue which you have addressed in this PR. Team will cherry pick the commits from your PR and may do further implementation to cover few more scenarios as needed. We will reach out to you if we need more information. For now, you can pause work on this PR.

Thank you once again!

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: PR in progress Progress: ready for grooming 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
Status: Ready for Development
Development

Successfully merging a pull request may close this issue.