[Issue] Sales rule CartFixed calculation : incorrect discount amount
Nuranto opened this issue · comments
Preconditions and environment
- Magento version 2.4.7
Steps to reproduce
- Create a sales rule "test1", without coupon, cart fixed amount = 5€ (Discard subsequent rules = no)
- Create a sales rule "test2", without coupon, cart fixed amount = 10€ (Discard subsequent rules = no)
- Create a sales rule "test3", with coupon 'test3', cart fixed amount = 1€ (Discard subsequent rules = no)
- Create a frontend cart, with total amount > 20€
- 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”.
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:
@magento give me 2.4-develop instance
- upcoming 2.4.x release- For more details, review the Magento Contributor Assistant documentation.
- Add a comment to assign the issue:
@magento I am working on this
- To learn more about issue processing workflow, refer to the Code Contributions.
Join Magento Community Engineering Slack and ask your questions in #github channel.
🕙 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.
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: 👇
-
- Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
-
- Verify that issue has a meaningful description and provides enough information to reproduce the issue.
-
- Add
Area: XXXXX
label to the ticket, indicating the functional areas it may be related to.
- Add
-
- Verify that the issue is reproducible on
2.4-develop
branchDetails
- Add the comment@magento give me 2.4-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on2.4-develop
branch, please, add the labelReproduced 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!
- Verify that the issue is reproducible on
- Join Magento Community Engineering Slack and ask your questions in #github channel.
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
branchDetails
- Add the comment@magento give me 2.4-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on2.4-develop
branch, please, add the labelReproduced 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.
@magento give me 2.4-develop instance
Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.
Hi @engcom-Dash, here is your Magento Instance: https://2763b33e65d4d0ac59042530bf393f86.instances-prod.magento-community.engineering
Admin access: https://2763b33e65d4d0ac59042530bf393f86.instances-prod.magento-community.engineering/admin_9966
Login: 49e06943
Password: a9af8f53016c
@magento give me 2.4-develop instance
Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.
Hi @engcom-Dash, here is your Magento Instance: https://2763b33e65d4d0ac59042530bf393f86.instances-prod.magento-community.engineering
Admin access: https://2763b33e65d4d0ac59042530bf393f86.instances-prod.magento-community.engineering/admin_fd99
Login: 6b3c2240
Password: dfa2d4f121d5
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
- Create a sales rule "test1", without coupon, cart fixed amount = 5€ (Discard subsequent rules = no)
- Create a sales rule "test2", without coupon, cart fixed amount = 10€ (Discard subsequent rules = no)
- Create a sales rule "test3", with coupon 'test3', cart fixed amount = 1€ (Discard subsequent rules = no)
- Create a frontend cart, with total amount > 20€
- Add coupon test3
Please refer the attached screenshots.
Discount amount after applying coupon code test3:
When cart amount/products are changed :
Cart rules Configuration:
✅ Jira issue https://jira.corp.adobe.com/browse/AC-11914 is successfully created for this GitHub issue.
✅ 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.
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.
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.