Skip to content

Commit 4afa4d1

Browse files
ENGCOM-3271: fixed Quote Item Prices are NULL in cart related events. #18685 #18808
2 parents 0b9017c + 541bc69 commit 4afa4d1

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

app/code/Magento/ConfigurableProduct/Setup/UpgradeData.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
use Magento\Catalog\Model\Product;
99
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
10-
use Magento\Framework\Setup\UpgradeDataInterface;
11-
use Magento\Framework\Setup\ModuleContextInterface;
12-
use Magento\Framework\Setup\ModuleDataSetupInterface;
1310
use Magento\Eav\Setup\EavSetup;
1411
use Magento\Eav\Setup\EavSetupFactory;
12+
use Magento\Framework\Setup\ModuleContextInterface;
13+
use Magento\Framework\Setup\ModuleDataSetupInterface;
14+
use Magento\Framework\Setup\UpgradeDataInterface;
1515

1616
/**
1717
* Upgrade Data script
@@ -62,6 +62,10 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
6262
}
6363
}
6464

65+
if (version_compare($context->getVersion(), '2.2.2', '<')) {
66+
$this->upgradeQuoteItemPrice($setup);
67+
}
68+
6569
$setup->endSetup();
6670
}
6771

@@ -97,4 +101,30 @@ private function updateRelatedProductTypes(string $attributeId, array $relatedPr
97101
implode(',', $relatedProductTypes)
98102
);
99103
}
104+
105+
/**
106+
* Update 'price' value for quote items without price of configurable products subproducts.
107+
*
108+
* @param ModuleDataSetupInterface $setup
109+
*/
110+
private function upgradeQuoteItemPrice(ModuleDataSetupInterface $setup)
111+
{
112+
$connection = $setup->getConnection();
113+
$quoteItemTable = $setup->getTable('quote_item');
114+
$select = $connection->select();
115+
$select->joinLeft(
116+
['qi2' => $quoteItemTable],
117+
'qi1.parent_item_id = qi2.item_id',
118+
['price']
119+
)->where(
120+
'qi1.price = 0'
121+
. ' AND qi1.parent_item_id IS NOT NULL'
122+
. ' AND qi2.product_type = "' . Configurable::TYPE_CODE . '"'
123+
);
124+
$updateQuoteItem = $setup->getConnection()->updateFromSelect(
125+
$select,
126+
['qi1' => $quoteItemTable]
127+
);
128+
$setup->getConnection()->query($updateQuoteItem);
129+
}
100130
}

app/code/Magento/ConfigurableProduct/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_ConfigurableProduct" setup_version="2.2.1">
9+
<module name="Magento_ConfigurableProduct" setup_version="2.2.2">
1010
<sequence>
1111
<module name="Magento_Catalog"/>
1212
<module name="Magento_Msrp"/>

app/code/Magento/Quote/Model/Quote/Item/Processor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function prepare(Item $item, DataObject $request, Product $candidate)
9797
$item->addQty($candidate->getCartQty());
9898

9999
$customPrice = $request->getCustomPrice();
100+
$item->setPrice($candidate->getFinalPrice());
100101
if (!empty($customPrice)) {
101102
$item->setCustomPrice($customPrice);
102103
$item->setOriginalCustomPrice($customPrice);

app/code/Magento/Quote/Test/Unit/Model/Quote/Item/ProcessorTest.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ protected function setUp()
7676
'addQty',
7777
'setCustomPrice',
7878
'setOriginalCustomPrice',
79-
'setData'
79+
'setData',
80+
'setprice'
8081
]);
8182
$this->quoteItemFactoryMock->expects($this->any())
8283
->method('create')
@@ -98,7 +99,13 @@ protected function setUp()
9899

99100
$this->productMock = $this->createPartialMock(
100101
\Magento\Catalog\Model\Product::class,
101-
['getCustomOptions', '__wakeup', 'getParentProductId', 'getCartQty', 'getStickWithinParent']
102+
[
103+
'getCustomOptions',
104+
'__wakeup',
105+
'getParentProductId',
106+
'getCartQty',
107+
'getStickWithinParent',
108+
'getFinalPrice']
102109
);
103110
$this->objectMock = $this->createPartialMock(
104111
\Magento\Framework\DataObject::class,
@@ -239,13 +246,17 @@ public function testPrepare()
239246
$customPrice = 400000000;
240247
$itemId = 1;
241248
$requestItemId = 1;
249+
$finalPrice = 1000000000;
242250

243251
$this->productMock->expects($this->any())
244252
->method('getCartQty')
245253
->will($this->returnValue($qty));
246254
$this->productMock->expects($this->any())
247255
->method('getStickWithinParent')
248256
->will($this->returnValue(false));
257+
$this->productMock->expects($this->once())
258+
->method('getFinalPrice')
259+
->will($this->returnValue($finalPrice));
249260

250261
$this->itemMock->expects($this->once())
251262
->method('addQty')
@@ -255,6 +266,9 @@ public function testPrepare()
255266
->will($this->returnValue($itemId));
256267
$this->itemMock->expects($this->never())
257268
->method('setData');
269+
$this->itemMock->expects($this->once())
270+
->method('setPrice')
271+
->will($this->returnValue($this->itemMock));
258272

259273
$this->objectMock->expects($this->any())
260274
->method('getCustomPrice')
@@ -282,13 +296,17 @@ public function testPrepareWithResetCountAndStick()
282296
$customPrice = 400000000;
283297
$itemId = 1;
284298
$requestItemId = 1;
299+
$finalPrice = 1000000000;
285300

286301
$this->productMock->expects($this->any())
287302
->method('getCartQty')
288303
->will($this->returnValue($qty));
289304
$this->productMock->expects($this->any())
290305
->method('getStickWithinParent')
291306
->will($this->returnValue(true));
307+
$this->productMock->expects($this->once())
308+
->method('getFinalPrice')
309+
->will($this->returnValue($finalPrice));
292310

293311
$this->itemMock->expects($this->once())
294312
->method('addQty')
@@ -298,6 +316,9 @@ public function testPrepareWithResetCountAndStick()
298316
->will($this->returnValue($itemId));
299317
$this->itemMock->expects($this->never())
300318
->method('setData');
319+
$this->itemMock->expects($this->once())
320+
->method('setPrice')
321+
->will($this->returnValue($this->itemMock));
301322

302323
$this->objectMock->expects($this->any())
303324
->method('getCustomPrice')
@@ -325,13 +346,17 @@ public function testPrepareWithResetCountAndNotStickAndOtherItemId()
325346
$customPrice = 400000000;
326347
$itemId = 1;
327348
$requestItemId = 2;
349+
$finalPrice = 1000000000;
328350

329351
$this->productMock->expects($this->any())
330352
->method('getCartQty')
331353
->will($this->returnValue($qty));
332354
$this->productMock->expects($this->any())
333355
->method('getStickWithinParent')
334356
->will($this->returnValue(false));
357+
$this->productMock->expects($this->once())
358+
->method('getFinalPrice')
359+
->will($this->returnValue($finalPrice));
335360

336361
$this->itemMock->expects($this->once())
337362
->method('addQty')
@@ -341,6 +366,9 @@ public function testPrepareWithResetCountAndNotStickAndOtherItemId()
341366
->will($this->returnValue($itemId));
342367
$this->itemMock->expects($this->never())
343368
->method('setData');
369+
$this->itemMock->expects($this->once())
370+
->method('setPrice')
371+
->will($this->returnValue($this->itemMock));
344372

345373
$this->objectMock->expects($this->any())
346374
->method('getCustomPrice')
@@ -368,6 +396,7 @@ public function testPrepareWithResetCountAndNotStickAndSameItemId()
368396
$customPrice = 400000000;
369397
$itemId = 1;
370398
$requestItemId = 1;
399+
$finalPrice = 1000000000;
371400

372401
$this->objectMock->expects($this->any())
373402
->method('getResetCount')
@@ -386,10 +415,16 @@ public function testPrepareWithResetCountAndNotStickAndSameItemId()
386415
$this->productMock->expects($this->any())
387416
->method('getStickWithinParent')
388417
->will($this->returnValue(false));
418+
$this->productMock->expects($this->once())
419+
->method('getFinalPrice')
420+
->will($this->returnValue($finalPrice));
389421

390422
$this->itemMock->expects($this->once())
391423
->method('addQty')
392424
->with($qty);
425+
$this->itemMock->expects($this->once())
426+
->method('setPrice')
427+
->will($this->returnValue($this->itemMock));
393428

394429
$this->objectMock->expects($this->any())
395430
->method('getCustomPrice')

dev/tests/api-functional/testsuite/Magento/Catalog/Api/CartItemRepositoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function testAddProductToCartWithCustomOptions()
7070
'custom_options' => $this->getOptions(),
7171
],
7272
],
73+
'price' => $item->getPrice(),
7374
],
7475
$response
7576
);

0 commit comments

Comments
 (0)