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 percent calculation when applying 2 and more sales rules by percent #37591

Open
1 of 5 tasks
dmrkato opened this issue Jun 7, 2023 · 13 comments · May be fixed by #39352
Open
1 of 5 tasks
Assignees
Labels
Area: Cart & Checkout Area: Framework Component: Weee Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P3 May be fixed according to the position in the backlog. Progress: PR in progress Reported on 2.4.6 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

@dmrkato
Copy link

dmrkato commented Jun 7, 2023

Preconditions and environment

  • Magento version: 2.4.6

Steps to reproduce

  1. Enable SKU attribute to use in promo rule conditions
    1.1 Go to the admin panel
    1.2 Go to Store -> Attributes -> Product
    1.3 Find the SKU product attribute and edit it
    1.4 Select Use for Promo Rule Conditions field to Yes value
    1.5 Save
  2. Create the first sales rule which applies 50% discount for all cart item
    2.1 Go to Marketing -> Promotions -> Cart Price Rules -> Add New Rule
    2.2 In Rule information section
    2.2.1 For Coupon field select No coupon value
    2.2.2 For Priority input 0 value
    2.3 In Actions section
    2.3.1 For Apply field select Percent of product price discount
    2.3.2 For Discount Amount field set 50 value
    2.3.3 Switch Discard subsequent rules to No value (need to apply few sales rules)

Capture 2023-06-07 at 22 08 39
3. Create a second sales rule which applies 50% discount for specific cart item by using coupon code
3.1 Go to Marketing -> Promotions -> Cart Price Rules -> Add New Rule
3.2 In Rule information section
3.2.1 For Coupon field select Specific coupon value
3.2.2 Set Coupon code field 1234
3.2.3 For Priority input 1 value

Capture 2023-06-07 at 22 21 24

3.3 In Actions section
3.3.1 For Apply field select Percent of product price discount
3.3.2 For Discount Amount field set 50 value
3.3.3 Fill Apply the rule only to cart items matching the following conditions to apply discount for specific product by SKU

Capture 2023-06-07 at 22 28 02

  1. Create plugin afterFormatPrice for the class Magento\Weee\Block\Item\Price\Renderer to display discount amount and discount percent in Shopping Cart
    <type name="Magento\Weee\Block\Item\Price\Renderer"> <plugin name="test_plugin_add_discount_amount_and_percent" type="Test\Test\Plugin\Tax\Block\Item\Price\RendererPlugin"/> </type>

`<?php
declare(strict_types=1);

namespace Test\Test\Plugin\Tax\Block\Item\Price;

use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Quote\Model\Quote\Item\AbstractItem as QuoteItem;
use Magento\Tax\Block\Item\Price\Renderer;

class RendererPlugin
{
public function __construct(
private readonly PriceCurrencyInterface $priceCurrency
) {
}

public function afterFormatPrice(
    Renderer $subject,
    $result
) {
    $item = $subject->getItem();
    if ($item instanceof QuoteItem
        && $subject->getNameInLayout() === 'checkout.item.price.row'
        && $item->getDiscountAmount() > 0
    ) {
        $result .= '<br/>' . implode(
            '<br/>',
            [
                'Final price:' . $this->priceCurrency->format($item->getPrice() - $item->getDiscountAmount()), // final price with discount
                'Discount %: ' . $item->getDiscountPercent(), // item percent discount
                'Discount amount:' . $this->priceCurrency->format($item->getDiscountAmount()) // item discount amount
            ]
        );
    }

    return $result;
}

}`

  1. Add product to cart on which can be applied coupon code
  2. Go to Shopping Cart
  3. Apply coupon code 1234

Expected result

On product applies 2 discount rules each have 50% discount so in general in must be 75%
Capture 2023-06-07 at 23 15 59

Actual result

Discount amount calculate correct - 750$
but Discount % calculate incorect it is 100%
image

Additional information

I find a possible solution for this issue
Need to update method _calculate in class Magento\SalesRule\Model\Rule\Action\Discount\ByPercent on 71 line

now code calculating $discountPercent just simply add a discount percent
$discountPercent = min(100, $item->getDiscountPercent() + $rulePercent);
and if update this line to
$discountPercent = min( 100, $item->getDiscountPercent() > 0 ? $item->getDiscountPercent() + (1-$item->getDiscountPercent()) * $_rulePct : $rulePercent );
the calculation will be correct

so method code must be like
`protected function _calculate($rule, $item, $qty, $rulePercent)
{
/** @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */
$discountData = $this->discountFactory->create();

    $itemPrice = $this->validator->getItemPrice($item);
    $baseItemPrice = $this->validator->getItemBasePrice($item);
    $itemOriginalPrice = $this->validator->getItemOriginalPrice($item);
    $baseItemOriginalPrice = $this->validator->getItemBaseOriginalPrice($item);

    $_rulePct = $rulePercent / 100;
    $discountData->setAmount(($qty * $itemPrice - $item->getDiscountAmount()) * $_rulePct);
    $discountData->setBaseAmount(($qty * $baseItemPrice - $item->getBaseDiscountAmount()) * $_rulePct);
    $discountData->setOriginalAmount(($qty * $itemOriginalPrice - $item->getDiscountAmount()) * $_rulePct);
    $discountData->setBaseOriginalAmount(
        ($qty * $baseItemOriginalPrice - $item->getBaseDiscountAmount()) * $_rulePct
    );

    if (!$rule->getDiscountQty() || $rule->getDiscountQty() >= $qty) {
        $discountPercent = min(100, $item->getDiscountPercent() + $rulePercent);
        $discountPercent = min(
            100,
            $item->getDiscountPercent() > 0 ?
                $item->getDiscountPercent() + (1-$item->getDiscountPercent()) * $_rulePct :
                $rulePercent
        );
        $item->setDiscountPercent($discountPercent);
    }

    return $discountData;
}`

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 Jun 7, 2023

Hi @3dWaRdeJ. 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.

@m2-assistant
Copy link

m2-assistant bot commented Jun 8, 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 Jun 8, 2023
@engcom-Bravo engcom-Bravo added Reported on 2.4.6 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 Jun 8, 2023
@engcom-November
Copy link
Contributor

@magento give me 2.4-develop instance

@magento-deployment-service
Copy link

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

@magento-deployment-service
Copy link

@engcom-Hotel engcom-Hotel self-assigned this Jul 10, 2023
@m2-assistant
Copy link

m2-assistant bot commented Jul 10, 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 @dmrkato,

Thanks for the report and collaboration!

We tried to reproduce the issue in the 2.4-develop branch and it seems the issue is reproducible for us. Please find below the screenshot for reference:

image

We have created a custom module in order to reproduce the issue. Please find below the same:
Magz.zip

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: Weee Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Priority: P3 May be fixed according to the position in the backlog. Area: Cart & Checkout Area: Framework and removed Issue: ready for confirmation labels Jul 10, 2023
@github-jira-sync-bot
Copy link

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

@m2-assistant
Copy link

m2-assistant bot commented Jul 10, 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.

@mcspronko
Copy link
Contributor

Does anyone work on this issue?

@dmrkato
Copy link
Author

dmrkato commented Nov 6, 2024

I don't know, in the task description I wrote a possible solution in the Addition information section

Need to write fix for calculation formula in Magento\SalesRule\Model\Rule\Action\Discount\ByPercent

P.S. @mcspronko thx for your videos on YouTube helped me a lot

@dmrkato
Copy link
Author

dmrkato commented Nov 6, 2024

I can write a solution
But I am not familiar how to create a branch for this issue and make pull reqest

@mcspronko
Copy link
Contributor

@dmrkato that would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Cart & Checkout Area: Framework Component: Weee Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P3 May be fixed according to the position in the backlog. Progress: PR in progress Reported on 2.4.6 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
Status: Ready for Development
Development

Successfully merging a pull request may close this issue.

7 participants