Skip to content

Commit f75a72d

Browse files
Roman HaninRoman Hanin
authored andcommitted
B2B-2243: Optimize addProductsToCart operations
1 parent 4b39700 commit f75a72d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,17 +1433,16 @@ public function getItemsCollection($useCache = true)
14331433
*/
14341434
public function getAllItems()
14351435
{
1436-
if (!empty($this->allItemsCache)) {
1437-
return $this->allItemsCache;
1438-
}
1436+
$items = [];
14391437
/** @var \Magento\Quote\Model\Quote\Item $item */
14401438
foreach ($this->getItemsCollection() as $item) {
14411439
$product = $item->getProduct();
14421440
if (!$item->isDeleted() && ($product && (int)$product->getStatus() !== ProductStatus::STATUS_DISABLED)) {
1443-
$this->allItemsCache[$product->getSku()] = $item;
1441+
$items[] = $item;
14441442
}
14451443
}
1446-
return $this->allItemsCache;
1444+
1445+
return $items;
14471446
}
14481447

14491448
/**
@@ -1578,7 +1577,7 @@ public function removeItem($itemId)
15781577
if ($parent) {
15791578
$parent->isDeleted(true);
15801579
}
1581-
unset($this->allItemsCache[$item->getSku()]);
1580+
15821581
$this->_eventManager->dispatch('sales_quote_remove_item', ['quote_item' => $item]);
15831582
}
15841583

@@ -1849,8 +1848,14 @@ public function updateItem($itemId, $buyRequest, $params = null)
18491848
*/
18501849
public function getItemByProduct($product)
18511850
{
1852-
$item = $this->getAllItems()[$product->getSku()] ?? null;
1853-
if ($item && $item->representProduct($product)) {
1851+
/** @var \Magento\Quote\Model\Quote\Item $item */
1852+
$item = $this->getItemsCollection()->getItemByColumnValue('product_id', $product->getId()) ?? null;
1853+
if ($item
1854+
&& !$item->isDeleted()
1855+
&& $item->getProduct()
1856+
&& $product->getStatus() !== ProductStatus::STATUS_DISABLED
1857+
&& $item->representProduct($product)
1858+
) {
18541859
return $item;
18551860
}
18561861
return false;

0 commit comments

Comments
 (0)