Skip to content

Commit b2e17a9

Browse files
committed
ACP2E-3498: Incorrect discount value when multiple cart price rules are applied simultaneously with discounted/special priced products
1 parent a6feed0 commit b2e17a9

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

app/code/Magento/SalesRule/Model/Rule/Action/Discount/ExistingDiscountRuleCollector.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77

88
namespace Magento\SalesRule\Model\Rule\Action\Discount;
99

10-
class ExistingDiscountRuleCollector
10+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
11+
12+
class ExistingDiscountRuleCollector implements ResetAfterRequestInterface
1113
{
1214
/**
1315
* @var array
1416
*/
1517
private array $ruleDiscounts = [];
1618

1719
/**
20+
* Store discounts that are applied to affected items by previous rules
21+
*
1822
* @param int $ruleId
1923
* @param float $discountAmount
2024
* @return void
@@ -25,11 +29,21 @@ public function setExistingRuleDiscount(int $ruleId, float $discountAmount): voi
2529
}
2630

2731
/**
32+
* Retrieve discount that was applied to affected items by previous rule
33+
*
2834
* @param int $ruleId
2935
* @return float|null
3036
*/
3137
public function getExistingRuleDiscount(int $ruleId): ?float
3238
{
3339
return $this->ruleDiscounts[$ruleId] ?? null;
3440
}
41+
42+
/**
43+
* @inheritDoc
44+
*/
45+
public function _resetState(): void
46+
{
47+
$this->ruleDiscounts = [];
48+
}
3549
}

app/code/Magento/SalesRule/Test/Unit/Model/Rule/Action/Discount/CartFixedTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\SalesRule\Model\Rule\Action\Discount\CartFixed;
2020
use Magento\SalesRule\Model\Rule\Action\Discount\Data;
2121
use Magento\SalesRule\Model\Rule\Action\Discount\DataFactory;
22+
use Magento\SalesRule\Model\Rule\Action\Discount\ExistingDiscountRuleCollector;
2223
use Magento\SalesRule\Model\Validator;
2324
use Magento\Store\Model\Store;
2425
use PHPUnit\Framework\MockObject\MockObject;
@@ -81,6 +82,11 @@ class CartFixedTest extends TestCase
8182
*/
8283
protected $cartFixedDiscountHelper;
8384

85+
/**
86+
* @var ExistingDiscountRuleCollector|MockObject
87+
*/
88+
private ExistingDiscountRuleCollector $existingDiscountRuleCollector;
89+
8490
/**
8591
* @inheritdoc
8692
*/
@@ -136,11 +142,16 @@ protected function setUp(): void
136142
->disableOriginalConstructor()
137143
->getMock();
138144

145+
$this->existingDiscountRuleCollector = $this->createMock(ExistingDiscountRuleCollector::class);
146+
$this->existingDiscountRuleCollector->expects($this->any())
147+
->method('getExistingRuleDiscount')
148+
->willReturn(0.00);
139149
$this->model = new CartFixed(
140150
$this->validator,
141151
$dataFactory,
142152
$this->priceCurrency,
143153
$this->deltaPriceRound,
154+
$this->existingDiscountRuleCollector,
144155
$this->cartFixedDiscountHelper
145156
);
146157
}
@@ -150,7 +161,7 @@ protected function setUp(): void
150161
* @dataProvider dataProviderActions
151162
* @param array $shipping
152163
* @param array $ruleDetails
153-
* @throws LocalizedException
164+
* @throws LocalizedException|\PHPUnit\Framework\MockObject\Exception
154165
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
155166
*/
156167
public function testCalculate(array $shipping, array $ruleDetails): void

0 commit comments

Comments
 (0)