Skip to content

Commit fc02f03

Browse files
author
Anna Bukatar
committed
ACP2E-680: Cart price rule fails to be applied until a change is triggered
1 parent e304792 commit fc02f03

File tree

2 files changed

+49
-58
lines changed
  • app/code/Magento/ConfigurableProduct

2 files changed

+49
-58
lines changed

app/code/Magento/ConfigurableProduct/Plugin/SalesRule/Model/Rule/Condition/Product.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1111

1212
/**
13-
* Class Product
14-
*
15-
* @package Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition
13+
* Plugin for Magento\SalesRule\Model\Rule\Condition\Product to apply rules on configurable children
1614
*/
1715
class Product
1816
{
@@ -57,6 +55,7 @@ private function getProductToValidate(
5755

5856
/* Check for attributes which are not available for configurable products */
5957
if ($product->getTypeId() == Configurable::TYPE_CODE &&
58+
$subject->getAttributeScope() !== 'parent' &&
6059
!$product->hasData($attrCode) &&
6160
count($model->getChildren())
6261
) {

app/code/Magento/ConfigurableProduct/Test/Unit/Plugin/SalesRule/Model/Rule/Condition/ProductTest.php

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Magento\SalesRule\Model\Rule\Condition\Product as SalesRuleProduct;
2929
use PHPUnit\Framework\MockObject\MockObject;
3030
use PHPUnit\Framework\TestCase;
31+
use Magento\Catalog\Model\ProductCategoryList;
3132

3233
/**
3334
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -114,6 +115,10 @@ private function createValidator(): SalesRuleProduct
114115
->getMock()
115116
);
116117

118+
$productCategoryList = $this->getMockBuilder(ProductCategoryList::class)
119+
->disableOriginalConstructor()
120+
->getMock();
121+
117122
return new SalesRuleProduct(
118123
$contextMock,
119124
$backendHelperMock,
@@ -122,60 +127,17 @@ private function createValidator(): SalesRuleProduct
122127
$productRepositoryMock,
123128
$productMock,
124129
$collectionMock,
125-
$formatMock
130+
$formatMock,
131+
[],
132+
$productCategoryList
126133
);
127134
}
128135

129136
public function testChildIsUsedForValidation()
130137
{
131-
$configurableProductMock = $this->createProductMock();
132-
$configurableProductMock
133-
->expects($this->any())
134-
->method('getTypeId')
135-
->willReturn(Configurable::TYPE_CODE);
136-
$configurableProductMock
137-
->expects($this->any())
138-
->method('hasData')
139-
->with('special_price')
140-
->willReturn(false);
141-
142-
/* @var AbstractItem|MockObject $item */
143-
$item = $this->getMockBuilder(AbstractItem::class)
144-
->disableOriginalConstructor()
145-
->setMethods(['setProduct', 'getProduct', 'getChildren'])
146-
->getMockForAbstractClass();
147-
$item->expects($this->any())
148-
->method('getProduct')
149-
->willReturn($configurableProductMock);
150-
151-
$simpleProductMock = $this->createProductMock();
152-
$simpleProductMock
153-
->expects($this->any())
154-
->method('getTypeId')
155-
->willReturn(Type::TYPE_SIMPLE);
156-
$simpleProductMock
157-
->expects($this->any())
158-
->method('hasData')
159-
->with('special_price')
160-
->willReturn(true);
161-
162-
$childItem = $this->getMockBuilder(AbstractItem::class)
163-
->disableOriginalConstructor()
164-
->setMethods(['getProduct'])
165-
->getMockForAbstractClass();
166-
$childItem->expects($this->any())
167-
->method('getProduct')
168-
->willReturn($simpleProductMock);
169-
170-
$item->expects($this->any())
171-
->method('getChildren')
172-
->willReturn([$childItem]);
173-
$item->expects($this->once())
174-
->method('setProduct')
175-
->with($simpleProductMock);
176-
138+
$item = $this->configurableProductTestSetUp();
139+
$item->expects($this->once())->method('setProduct');
177140
$this->validator->setAttribute('special_price');
178-
179141
$this->validatorPlugin->beforeValidate($this->validator, $item);
180142
}
181143

@@ -209,8 +171,28 @@ private function createProductMock(): MockObject
209171
return $productMock;
210172
}
211173

212-
public function testChildIsNotUsedForValidation()
174+
public function configurableProductTestSetUp()
213175
{
176+
$configurableProductMock = $this->createProductMock();
177+
$configurableProductMock
178+
->expects($this->any())
179+
->method('getTypeId')
180+
->willReturn(Configurable::TYPE_CODE);
181+
$configurableProductMock
182+
->expects($this->any())
183+
->method('hasData')
184+
->with('special_price')
185+
->willReturn(false);
186+
187+
/* @var AbstractItem|MockObject $item */
188+
$item = $this->getMockBuilder(AbstractItem::class)
189+
->disableOriginalConstructor()
190+
->setMethods(['setProduct', 'getProduct', 'getChildren'])
191+
->getMockForAbstractClass();
192+
$item->expects($this->any())
193+
->method('getProduct')
194+
->willReturn($configurableProductMock);
195+
214196
$simpleProductMock = $this->createProductMock();
215197
$simpleProductMock
216198
->expects($this->any())
@@ -222,17 +204,27 @@ public function testChildIsNotUsedForValidation()
222204
->with('special_price')
223205
->willReturn(true);
224206

225-
/* @var AbstractItem|MockObject $item */
226-
$item = $this->getMockBuilder(AbstractItem::class)
207+
$childItem = $this->getMockBuilder(AbstractItem::class)
227208
->disableOriginalConstructor()
228-
->setMethods(['setProduct', 'getProduct'])
209+
->setMethods(['getProduct'])
229210
->getMockForAbstractClass();
230-
$item->expects($this->any())
211+
$childItem->expects($this->any())
231212
->method('getProduct')
232213
->willReturn($simpleProductMock);
233214

234-
$this->validator->setAttribute('special_price');
215+
$item->expects($this->any())
216+
->method('getChildren')
217+
->willReturn([$childItem]);
218+
219+
return $item;
220+
}
235221

222+
public function testChildIsNotUsedForValidation()
223+
{
224+
$item = $this->configurableProductTestSetUp();
225+
$item->expects($this->never())->method('setProduct');
226+
$this->validator->setAttribute('special_price');
227+
$this->validator->setAttributeScope('parent');
236228
$this->validatorPlugin->beforeValidate($this->validator, $item);
237229
}
238230

@@ -266,7 +258,7 @@ public function testChildIsNotUsedForValidationWhenConfigurableProductIsMissingC
266258
->willReturn([]);
267259

268260
$this->validator->setAttribute('special_price');
269-
261+
$item->expects($this->never())->method('setProduct');
270262
$this->validatorPlugin->beforeValidate($this->validator, $item);
271263
}
272264
}

0 commit comments

Comments
 (0)