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

Cart price rules - Incorrect discount amount when using fixed discount amount for whole cart #37496

Closed
1 of 5 tasks
vandersonramoslybe opened this issue May 12, 2023 · 18 comments · Fixed by #37730
Closed
1 of 5 tasks
Assignees
Labels
Area: Cart & Checkout Component: Rule Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: done Reported on 2.4.x Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@vandersonramoslybe
Copy link

Preconditions and environment

  • Magento version: 2.3., 2.4.

Steps to reproduce

Create a Cart price rule:

1 - Marketing -> Promotions -> Cart Price Rules

Conditions:

Screenshot 2023-05-12 at 15 11 48

2 - Actions:

Screenshot 2023-05-12 at 15 13 27

It is important to be: Fixed amount discount for whole cart

3 - Create a plugin afterGetPrice for the class Magento\Quote\Model\Quote\Item. method getPrice()

<type name="Magento\Quote\Model\Quote\Item">
    <plugin name="my_plugin_name" type="Vendor\Module\Plugin\GetPriceFromQuoteItemPlugin" />
</type>

EXAMPLE:

`<?php

declare(strict_types=1);

namespace Vendor\Module\Plugin;

use Magento\Quote\Model\Quote\Item as QuoteItem;

class GetPriceFromQuoteItemPlugin
{

public function afterGetPrice(QuoteItem $subject, $result): float
{
    // ADD THE CHANGES HERE....
    
    $result = 1070.0;

    return $result;
}

}`

Choose a configurable product, and add it to the cart. The price will be changed to the new one, after that, apply the coupon code:

Screenshot 2023-05-12 at 14 30 02

Check the discount value.

Expected result

It should be 500.00, not 250.00

Actual result

It is 250.00

Additional information

No response

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”.
@m2-assistant
Copy link

m2-assistant bot commented May 12, 2023

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

@Chris-Kiddier
Copy link

Hi,

I've recently experience this problem after upgrading to 2.4.5. It only happens when I try and apply a Fixed amount discount coupon to a quote with a configurable product in it.
For example, a £5 discount will amount to a £2.50 discount when applied to the configurable product.

Considering it's exactly half, my first thought was that the discount is somehow applying to both the Parent and Child product, however, as only 1 of those is taken into account when calculating row totals, it only takes into account half the discount.

More evidence for that is from a self developed module that applies loyalty points to products. Only since 2.4.5 has it started applying double the amount to configurable products, as if there were two products in 1 row.

Maybe that will help, but I'll be keeping an eye on this issue as well!

@vandersonramoslybe
Copy link
Author

Hi,

I've recently experience this problem after upgrading to 2.4.5. It only happens when I try and apply a Fixed amount discount coupon to a quote with a configurable product in it. For example, a £5 discount will amount to a £2.50 discount when applied to the configurable product.

Considering it's exactly half, my first thought was that the discount is somehow applying to both the Parent and Child product, however, as only 1 of those is taken into account when calculating row totals, it only takes into account half the discount.

More evidence for that is from a self developed module that applies loyalty points to products. Only since 2.4.5 has it started applying double the amount to configurable products, as if there were two products in 1 row.

Maybe that will help, but I'll be keeping an eye on this issue as well!

I tested on Magento 2.3.7 as well, same behavior.

This is the class used when we have a cart rule as a Whole cart: https://github.com/magento/magento2/blob/2.4.6/app/code/Magento/SalesRule/Model/Rule/Action/Discount/CartFixed.php

The value on line 82 $baseRuleTotals = $ruleTotals['base_items_price'] ?? 0.0; contains the price for 2 items instead of only one as expected. For some reason, the method isChildrenCalculated from https://github.com/magento/magento2/blob/2.4.5-p2/app/code/Magento/SalesRule/Model/Validator.php is interfering with the calculation

@chernenm
Copy link

@magento give me 2.4-develop instance

@magento-deployment-service
Copy link

Hi @chernenm. Thank you for your request. I'm working on Magento instance for you.

@magento-deployment-service
Copy link

@engcom-Dash engcom-Dash self-assigned this May 12, 2023
@m2-assistant
Copy link

m2-assistant bot commented May 12, 2023

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-Dash engcom-Dash removed their assignment May 12, 2023
@engcom-Bravo engcom-Bravo added Reported on 2.4.x Indicates original Magento version for the Issue report. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it labels May 15, 2023
@Chris-Kiddier
Copy link

I had a nosey through Validator.php. I'm not sure what constitutes a child item as 'calculated' and considering how often that method is referenced I was hesitant to change anything.

However, I added an extra line to the if statement on line 464 of the Validator class (function initTotals()) and it seems to have fixed the issue for me, calculating the fixed amount discount correctly when I go to checkout.

if (!$this->isValidItemForRule($item, $rule)
    || ($item->getChildren() && $item->isChildrenCalculated())
    || $item->getNoDiscount()
    || ($item->getProductType() == "simple" && $item->getParentItemId() != null) // New line added
) {
    continue;
}

I figured, as the statement seems to be including Simple products that are also children, they should be omitted from the calculation.
This is not elegant by any means (and involves editing core), but it has provided some functionality back to my dev site.

I'll await any further info, currently this is staying safely away from production

@abdul0231
Copy link

I had proposed a simple fix through Validator.php. I am also interrupting this above if statement but I'm excluding a check

$item->isChildrenCalculated()

The updated code is below

foreach ($items as $item) { if (!$this->isValidItemForRule($item, $rule) || ($item->getChildren()) || $item->getNoDiscount() ) { continue; }

I have seen that parent and child were getting calculated and amount was getting double the accurate price and then the ratio calculated 0.5 causing discount to get half.

I'm not very 100 percent sure this will be optimal solution but I have tested all the scenarios with loggedin and non logged in customers and every possible test case. Everything works fine.

@engcom-Hotel engcom-Hotel self-assigned this May 24, 2023
@m2-assistant
Copy link

m2-assistant bot commented May 24, 2023

Hi @engcom-Hotel. 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-Hotel
Copy link
Contributor

Hello @vandersonramoslybe,

Thanks for the report and collaboration!

We have tried to reproduce the issue with the mentioned steps in the main description. We have used the 2.4-develop branch in order to reproduce the issue. But for us, the discount amount is showing $500. Please have a look to the below screenshot for reference:

image

Please review the steps and let us know if we need to do any additional steps in order to reproduce the issue.

Thanks

@engcom-Hotel engcom-Hotel added the Issue: needs update Additional information is require, waiting for response label May 24, 2023
@vandersonramoslybe
Copy link
Author

Hi @engcom-Hotel

Do you have a plugin as explained in the description? Step 3

3 - Create a plugin afterGetPrice for the class Magento\Quote\Model\Quote\Item. method getPrice()

@engcom-Hotel
Copy link
Contributor

Yes @vandersonramoslybe,

Let me attach the same here for reference:

Magz.zip

Please let me know if we missed anything.

Thanks

@vandersonramoslybe
Copy link
Author

Hi @engcom-Hotel

Thanks, I installed your plugin. I am running a vanilla Magento 2.4.6. The issue is reproducible with Configurable products.

Screenshot 2023-05-25 at 09 24 59

Screenshot 2023-05-25 at 09 29 25

test-Promotions-Marketing-Magento-Admin

Screenshot 2023-05-25 at 09 28 25

Screenshot 2023-05-25 at 09 28 38

Screenshot 2023-05-25 at 09 28 52

@engcom-Hotel
Copy link
Contributor

Hello @vandersonramoslybe,

Thanks for the response!

Now we are able to reproduce the issue. Please refer to the below screenshot for reference:

image

Hence confirming the issue.

Thanks

@engcom-Hotel engcom-Hotel added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Component: Rule Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Priority: P2 A defect with this priority could have functionality issues which are not to expectations. and removed Issue: needs update Additional information is require, waiting for response labels May 26, 2023
@github-jira-sync-bot
Copy link

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

@m2-assistant
Copy link

m2-assistant bot commented May 26, 2023

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

@aasim110
Copy link
Contributor

aasim110 commented Jul 6, 2023

@magento I'm working on this

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Cart & Checkout Component: Rule Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: done Reported on 2.4.x Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Development

Successfully merging a pull request may close this issue.

9 participants