Skip to content

Commit 3e7c7c8

Browse files
committed
MAGETWO-83169: Loaded quote items collection contains removed product items
1 parent 8de80d7 commit 3e7c7c8

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

app/code/Magento/Quote/Model/ResourceModel/Quote/Item/Collection.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,7 @@ protected function _afterLoad()
169169
$productIds[] = (int)$item->getProductId();
170170
}
171171
$this->_productIds = array_merge($this->_productIds, $productIds);
172-
$productCollection = $this->_productCollectionFactory->create()->addIdFilter($this->_productIds);
173-
$existingProductsIds = $productCollection->getAllIds();
174-
$absentProductsIds = array_diff($this->_productIds, $existingProductsIds);
175-
// Remove not existing products from items collection
176-
if (!empty($absentProductsIds)) {
177-
foreach ($absentProductsIds as $productIdToExclude) {
178-
/** @var \Magento\Quote\Model\Quote\Item $quoteItem */
179-
$quoteItem = $this->getItemByColumnValue('product_id', $productIdToExclude);
180-
$this->removeItemByKey($quoteItem->getId());
181-
}
182-
}
183-
172+
$this->removeItemsWithAbsentProducts();
184173
/**
185174
* Assign options and products
186175
*/
@@ -218,12 +207,6 @@ protected function _assignOptions()
218207
protected function _assignProducts()
219208
{
220209
\Magento\Framework\Profiler::start('QUOTE:' . __METHOD__, ['group' => 'QUOTE', 'method' => __METHOD__]);
221-
$productIds = [];
222-
foreach ($this as $item) {
223-
$productIds[] = (int)$item->getProductId();
224-
}
225-
$this->_productIds = array_merge($this->_productIds, $productIds);
226-
227210
$productCollection = $this->_productCollectionFactory->create()->setStoreId(
228211
$this->getStoreId()
229212
)->addIdFilter(
@@ -318,4 +301,24 @@ private function addTierPriceData(ProductCollection $productCollection)
318301
$productCollection->addTierPriceDataByGroupId($this->_quote->getCustomerGroupId());
319302
}
320303
}
304+
305+
/**
306+
* Find and remove quote items with non existing products
307+
*
308+
* @return void
309+
*/
310+
private function removeItemsWithAbsentProducts()
311+
{
312+
$productCollection = $this->_productCollectionFactory->create()->addIdFilter($this->_productIds);
313+
$existingProductsIds = $productCollection->getAllIds();
314+
$absentProductsIds = array_diff($this->_productIds, $existingProductsIds);
315+
// Remove not existing products from items collection
316+
if (!empty($absentProductsIds)) {
317+
foreach ($absentProductsIds as $productIdToExclude) {
318+
/** @var \Magento\Quote\Model\Quote\Item $quoteItem */
319+
$quoteItem = $this->getItemByColumnValue('product_id', $productIdToExclude);
320+
$this->removeItemByKey($quoteItem->getId());
321+
}
322+
}
323+
}
321324
}

0 commit comments

Comments
 (0)