Skip to content

[Forwardport] fixed Quote Item Prices are NULL in cart related events. #18685 #18806

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ConfigurableProduct\Setup\Patch\Data;

use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;

/**
* Update quote_item table 'price' column data.
*/
class UpdateQuoteItemPrice implements DataPatchInterface, PatchVersionInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;

/**
* @param ModuleDataSetupInterface $moduleDataSetup
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup
) {
$this->moduleDataSetup = $moduleDataSetup;
}

/**
* @inheritdoc
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->beginTransaction();
try {
$this->applyPatch();
$this->moduleDataSetup->getConnection()->commit();
} catch (\Exception $e) {
$this->moduleDataSetup->getConnection()->rollBack();
throw $e;
}
}

/**
* Update 'price' value for quote items without price of configurable products subproducts.
*
* @return void
*/
private function applyPatch(): void
{
$connection = $this->moduleDataSetup->getConnection();
$quoteItemTable = $this->moduleDataSetup->getTable('quote_item');
$select = $connection->select();
$select->joinLeft(
['qi2' => $quoteItemTable],
'qi1.parent_item_id = qi2.item_id',
['price']
)->where(
'qi1.price = 0'
. ' AND qi1.parent_item_id IS NOT NULL'
. ' AND qi2.product_type = "' . Configurable::TYPE_CODE . '"'
);
$updateQuoteItem = $this->moduleDataSetup->getConnection()->updateFromSelect(
$select,
['qi1' => $quoteItemTable]
);
$this->moduleDataSetup->getConnection()->query($updateQuoteItem);
}

/**
* @inheritdoc
*/
public static function getDependencies()
{
return [];
}

/**
* @inheritdoc
*/
public static function getVersion()
{
return '2.2.2';
}

/**
* @inheritdoc
*/
public function getAliases()
{
return [];
}
}
4 changes: 2 additions & 2 deletions app/code/Magento/Quote/Model/Quote/Item/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function __construct(
/**
* Initialize quote item object
*
* @param DataObject $request
* @param Product $product
* @param DataObject $request
*
* @return Item
*/
Expand Down Expand Up @@ -95,7 +95,7 @@ public function prepare(Item $item, DataObject $request, Product $candidate): vo
$item->setData(CartItemInterface::KEY_QTY, 0);
}
$item->addQty($candidate->getCartQty());

$item->setPrice($candidate->getFinalPrice());
$customPrice = $request->getCustomPrice();
if (!empty($customPrice)) {
$item->setCustomPrice($customPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ protected function setUp()
'addQty',
'setCustomPrice',
'setOriginalCustomPrice',
'setData'
'setData',
'setprice'
]);
$this->quoteItemFactoryMock->expects($this->any())
->method('create')
Expand All @@ -98,7 +99,14 @@ protected function setUp()

$this->productMock = $this->createPartialMock(
\Magento\Catalog\Model\Product::class,
['getCustomOptions', '__wakeup', 'getParentProductId', 'getCartQty', 'getStickWithinParent']
[
'getCustomOptions',
'__wakeup',
'getParentProductId',
'getCartQty',
'getStickWithinParent',
'getFinalPrice'
]
);
$this->objectMock = $this->createPartialMock(
\Magento\Framework\DataObject::class,
Expand Down Expand Up @@ -239,13 +247,16 @@ public function testPrepare()
$customPrice = 400000000;
$itemId = 1;
$requestItemId = 1;

$finalPrice = 1000000000;
$this->productMock->expects($this->any())
->method('getCartQty')
->will($this->returnValue($qty));
$this->productMock->expects($this->any())
->method('getStickWithinParent')
->will($this->returnValue(false));
$this->productMock->expects($this->once())
->method('getFinalPrice')
->will($this->returnValue($finalPrice));

$this->itemMock->expects($this->once())
->method('addQty')
Expand All @@ -255,6 +266,9 @@ public function testPrepare()
->will($this->returnValue($itemId));
$this->itemMock->expects($this->never())
->method('setData');
$this->itemMock->expects($this->once())
->method('setPrice')
->will($this->returnValue($this->itemMock));

$this->objectMock->expects($this->any())
->method('getCustomPrice')
Expand Down Expand Up @@ -282,13 +296,17 @@ public function testPrepareWithResetCountAndStick()
$customPrice = 400000000;
$itemId = 1;
$requestItemId = 1;
$finalPrice = 1000000000;

$this->productMock->expects($this->any())
->method('getCartQty')
->will($this->returnValue($qty));
$this->productMock->expects($this->any())
->method('getStickWithinParent')
->will($this->returnValue(true));
$this->productMock->expects($this->once())
->method('getFinalPrice')
->will($this->returnValue($finalPrice));

$this->itemMock->expects($this->once())
->method('addQty')
Expand All @@ -298,6 +316,9 @@ public function testPrepareWithResetCountAndStick()
->will($this->returnValue($itemId));
$this->itemMock->expects($this->never())
->method('setData');
$this->itemMock->expects($this->once())
->method('setPrice')
->will($this->returnValue($this->itemMock));

$this->objectMock->expects($this->any())
->method('getCustomPrice')
Expand Down Expand Up @@ -325,13 +346,17 @@ public function testPrepareWithResetCountAndNotStickAndOtherItemId()
$customPrice = 400000000;
$itemId = 1;
$requestItemId = 2;
$finalPrice = 1000000000;

$this->productMock->expects($this->any())
->method('getCartQty')
->will($this->returnValue($qty));
$this->productMock->expects($this->any())
->method('getStickWithinParent')
->will($this->returnValue(false));
$this->productMock->expects($this->once())
->method('getFinalPrice')
->will($this->returnValue($finalPrice));

$this->itemMock->expects($this->once())
->method('addQty')
Expand All @@ -341,6 +366,9 @@ public function testPrepareWithResetCountAndNotStickAndOtherItemId()
->will($this->returnValue($itemId));
$this->itemMock->expects($this->never())
->method('setData');
$this->itemMock->expects($this->once())
->method('setPrice')
->will($this->returnValue($this->itemMock));

$this->objectMock->expects($this->any())
->method('getCustomPrice')
Expand Down Expand Up @@ -368,6 +396,7 @@ public function testPrepareWithResetCountAndNotStickAndSameItemId()
$customPrice = 400000000;
$itemId = 1;
$requestItemId = 1;
$finalPrice = 1000000000;

$this->objectMock->expects($this->any())
->method('getResetCount')
Expand All @@ -386,10 +415,16 @@ public function testPrepareWithResetCountAndNotStickAndSameItemId()
$this->productMock->expects($this->any())
->method('getStickWithinParent')
->will($this->returnValue(false));
$this->productMock->expects($this->once())
->method('getFinalPrice')
->will($this->returnValue($finalPrice));

$this->itemMock->expects($this->once())
->method('addQty')
->with($qty);
$this->itemMock->expects($this->once())
->method('setPrice')
->will($this->returnValue($this->itemMock));

$this->objectMock->expects($this->any())
->method('getCustomPrice')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Api;

use Magento\TestFramework\TestCase\WebapiAbstract;
Expand Down Expand Up @@ -58,18 +59,20 @@ public function testAddProductToCartWithCustomOptions()
$item = $quote->getAllItems()[0];
$this->assertEquals(
[
'item_id' => $item->getItemId(),
'item_id' => (int) $item->getItemId(),
'sku' => $item->getSku(),
'qty' => $item->getQty(),
'name' => $item->getName(),

'price' => $item->getPrice(),

'product_type' => $item->getProductType(),
'quote_id' => $item->getQuoteId(),
'product_option' => [
'extension_attributes' => [
'custom_options' => $this->getOptions(),
],
],
'price' => $item->getPrice(),
],
$response
);
Expand All @@ -80,7 +83,7 @@ public function testAddProductToCartWithCustomOptions()
*/
public function testGetList()
{
/** @var \Magento\Quote\Model\Quote $quote */
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
$quote->load('test_order_1', 'reserved_order_id');
$cartId = $quote->getId();
Expand Down