Skip to content

magento/magento2#39036: Issue with multiple percentage discounts #39683

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

Open
wants to merge 7 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

quterorta
Copy link
Contributor

@quterorta quterorta commented Feb 28, 2025

Description (*)

The issue was related to the discount percent calculation in the foreach iteration.
So, when we have multiple rules in the foreach function we apply these rules one-by-one, so discounts we apply one-by-one too, and just summing them is wrong, so I used the next formula for the discount percentage calculation: (X+Y) - ((X*Y) / 100).

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes: Issue with multiple percentage discounts #39036

Manual testing scenarios (*)

  1. Create a couple of cart rules (or more than 2 rules) with the next params:
Rule name: {your rule name}
Conditions: Apply for all product
Action: 
    Apply: Percent of product price discount
    Discount Amount: {some discount amount}
    Discard subsequent rules: No
  1. Add some products to the cart and place an order.
  2. After that, get order data via the next API REST endpoint: rest/V1/orders/{order_id}
  3. Check the discount_percent field for the item
  4. Calculate the price after discount by yourself: {base_price} * ((100-{discount_percent})/100)
  5. Calculated price must be equal to the {row_total} value

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Copy link

m2-assistant bot commented Feb 28, 2025

Hi @quterorta. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@m2-github-services m2-github-services added Partner: Perspective partners-contribution Pull Request is created by Magento Partner labels Feb 28, 2025
@quterorta quterorta changed the title Fix for issue 39036 magento/magento2#39036: Issue with multiple percentage discounts Feb 28, 2025
@engcom-Bravo engcom-Bravo added the Priority: P3 May be fixed according to the position in the backlog. label Mar 3, 2025
@quterorta
Copy link
Contributor Author

@magento run all tests

@Priyakshic Priyakshic added the Project: Community Picked PRs upvoted by the community label Mar 13, 2025
@Priyakshic Priyakshic moved this to Pending Review in Community Dashboard Mar 13, 2025
@engcom-Hotel
Copy link
Contributor

@magento run all tests

@engcom-Hotel engcom-Hotel self-requested a review March 28, 2025 05:00
Copy link
Contributor

@engcom-Hotel engcom-Hotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @quterorta,

Thanks for the contribution!

The changes seems good to me, but I guess we need to adjust the unit test according to this change. And also please fix the failed static tests.

Thanks

@engcom-Hotel engcom-Hotel moved this from Pending Review to Changes Requested in Community Dashboard Mar 28, 2025
@engcom-Dash engcom-Dash self-assigned this Apr 1, 2025
@engcom-Dash
Copy link
Contributor

@quterorta I am taking care of the test case fixes. Thank You!

@engcom-Dash
Copy link
Contributor

@magento run all tests

@engcom-Dash
Copy link
Contributor

@magento run Unit Tests, Static Tests

@engcom-Dash engcom-Dash moved this from Changes Requested to Pending Review in Community Dashboard Apr 2, 2025
@engcom-Hotel engcom-Hotel moved this from Pending Review to Review in Progress in Community Dashboard Apr 4, 2025
@engcom-Hotel
Copy link
Contributor

@magento run all tests

@engcom-Hotel engcom-Hotel moved this from Review in Progress to Ready for Testing in Community Dashboard Apr 4, 2025
@engcom-Bravo engcom-Bravo moved this from Ready for Testing to Testing in Progress in Community Dashboard Apr 7, 2025
@engcom-Bravo
Copy link
Contributor

Hi @quterorta,

Thanks for the collaboration & contribution!

✔️ QA Passed

Preconditions:

  • Install fresh Magento 2.4-develop

Steps to reproduce

  • Create a couple of cart rules (or more than 2 rules) with the next params:
  • Rule name: {your rule name}
  • Conditions: Apply for all product
  • Action:
  • Apply: Percent of product price discount
    
  • Discount Amount: {some discount amount}
    
  • Discard subsequent rules: No
    
  • Add some products to the cart and place an order.
  • After that, get order data via the next API REST endpoint: rest/V1/orders/{order_id}
  • Check the discount_percent field for the item
  • Calculate the price after discount by yourself: {base_price} * ((100-{discount_percent})/100)
  • Calculated price must be equal to the {row_total} value

Before: ✖️ 

Screenshot 2025-04-07 at 12 12 17 pm

After: ✔️

Screenshot 2025-04-07 at 12 18 53 pm

Builds are failed. Hence, moving this PR to Extended Testing.

Thanks.

@engcom-Bravo engcom-Bravo moved this from Testing in Progress to Extended testing (optional) in Community Dashboard Apr 7, 2025
@engcom-Dash
Copy link
Contributor

@magento run all tests

@engcom-Dash
Copy link
Contributor

@magento run Functional Tests B2B, Functional Tests CE, Functional Tests EE

@engcom-Dash
Copy link
Contributor

Two of the Functional EE Test failures are inconsistent and seems to be flaky. They neither part of PR nor failing because of the PR changes. The consistent failure is a known issue.

Functional Tests EE:

Build 1: Allure Report - EE

image

Build 2: Allure Report - EE

image

Known Issue:

StorefrontAssertAsLowAsLabelTest: ACQE-7645

Hence Moving this PR in Merge In Progress

@engcom-Dash engcom-Dash moved this from Extended testing (optional) to Merge in Progress in Community Dashboard Apr 8, 2025
Copy link
Contributor

@Den4ik Den4ik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that the issue lies in how the discount amount is calculated, not the percentage itself.
Let me give an example.

Suppose we have a product priced at $100 and two price rules:

  1. 10% discount
  2. 5% discount

In the Cart Price Rule settings, the Percent of product price discount option is selected.
This setting implies that the discount should be calculated based on the original product price.
So I would expect the final discount to be:

(10% of $100) + (5% of $100) = $10 + $5 = $15

However, the current implementation calculates it like this:

  1. 10% of $100 = $10
  2. 5% of ($100 - $10) = 5% of $90 = $4.5

There is no mention in the documentation that the discount is calculated from the cart item total rather than the original product price,
or that on each iteration of applying a Cart Price Rule, the cart item row total (i.e., product price minus all previously applied rule discounts) is used instead.

@Den4ik
Copy link
Contributor

Den4ik commented Apr 8, 2025

cc @sidolov @engcom-Hotel what do you think?

@engcom-Hotel
Copy link
Contributor

Hello @Den4ik,

You are right, the current discount calculation is working as below:

However, the current implementation calculates it like this:

  1. 10% of $100 = $10
  2. 5% of ($100 - $10) = 5% of $90 = $4.5

Please refer to the below screenshot from this page, which says the same thing on discount calculation:
image

@Den4ik
Copy link
Contributor

Den4ik commented Apr 10, 2025

Hi @engcom-Hotel
But I didn't find in documentation any information that second rule applies to the discounted price (after rule 1). Documentation mention only product price.
In my opinion product price and cart item price (after applying discount) are not the same.

@engcom-Hotel
Copy link
Contributor

Moving this pull request (PR) to On Hold status due to the ongoing discussion.

@engcom-Hotel engcom-Hotel moved this from Merge in Progress to On Hold in Community Dashboard Apr 11, 2025
@engcom-Hotel
Copy link
Contributor

Hello @Den4ik,

As per our discussion on Slack, your concern is regarding the calculation of two cart price rules, 10% and 5%, applied as Percent of product price discount. Instead of the following calculation:

  1. 10% of $100 = $10
  2. 5% of ($100 - $10) = 5% of $90 = $4.5
  3. Final discounted Price $90 - $4.5 = $85.5

It should calculate as below:

  1. 10% + 5% = 15%
  2. 15% of $100 = $15
  3. Final discounted Price $100 - $15 = $85

Please confirm, if I understood correct.

Thanks

@Den4ik
Copy link
Contributor

Den4ik commented Apr 21, 2025

Hi @engcom-Hotel

Please confirm, if I understood correct.

You understood correctly. I see 2 possible solutions:

  1. Update documentation and mention that next rules apply to cart item price after previous rules
  2. Update logic and use product price for all rules

@engcom-Hotel
Copy link
Contributor

Hello @Den4ik,

We are currently discussing this with the internal team and will update you accordingly.

Thanks

@engcom-Hotel engcom-Hotel added the Triage: Need PO Confirmation Requirements should be clarified/approved/confirmed with Product Manager. Not ready for fix/delivery label Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Partner: Perspective partners-contribution Pull Request is created by Magento Partner Priority: P3 May be fixed according to the position in the backlog. Progress: on hold Project: Community Picked PRs upvoted by the community Triage: Need PO Confirmation Requirements should be clarified/approved/confirmed with Product Manager. Not ready for fix/delivery
Projects
Status: On Hold
Development

Successfully merging this pull request may close these issues.

Issue with multiple percentage discounts
7 participants