magento2: Cart price rules - Incorrect discount amount when using fixed discount amount for whole cart

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()

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\Quote\Item">
    <plugin name="my_plugin_name" type="Vendor\Module\Plugin\GetPriceFromQuoteItemPlugin" />
</type>
</config>

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

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 18 (4 by maintainers)

Most upvoted comments

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