Skip to content

Commit 791f487

Browse files
committed
MAGETWO-83631: [Backport for 2.1.x] Loaded quote items collection contains removed product items
1 parent 137d92b commit 791f487

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,6 @@ public function resetJoinQuotes($quotesTableName, $productId = null)
141141
return $this;
142142
}
143143

144-
/**
145-
* Join product entities to select existing products items only
146-
*
147-
* @return void
148-
*/
149-
protected function _beforeLoad()
150-
{
151-
parent::_beforeLoad();
152-
$this->join(
153-
['cpe' => $this->getResource()->getTable('catalog_product_entity')],
154-
"cpe.entity_id = main_table.product_id",
155-
[]
156-
);
157-
}
158-
159144
/**
160145
* After load processing
161146
*
@@ -165,18 +150,20 @@ protected function _afterLoad()
165150
{
166151
parent::_afterLoad();
167152

168-
/**
169-
* Assign parent items
170-
*/
153+
$productIds = [];
171154
foreach ($this as $item) {
155+
// Assign parent items
172156
if ($item->getParentItemId()) {
173157
$item->setParentItem($this->getItemById($item->getParentItemId()));
174158
}
175159
if ($this->_quote) {
176160
$item->setQuote($this->_quote);
177161
}
162+
// Collect quote products ids
163+
$productIds[] = (int)$item->getProductId();
178164
}
179-
165+
$this->_productIds = array_merge($this->_productIds, $productIds);
166+
$this->removeItemsWithAbsentProducts();
180167
/**
181168
* Assign options and products
182169
*/
@@ -214,12 +201,6 @@ protected function _assignOptions()
214201
protected function _assignProducts()
215202
{
216203
\Magento\Framework\Profiler::start('QUOTE:' . __METHOD__, ['group' => 'QUOTE', 'method' => __METHOD__]);
217-
$productIds = [];
218-
foreach ($this as $item) {
219-
$productIds[] = (int)$item->getProductId();
220-
}
221-
$this->_productIds = array_merge($this->_productIds, $productIds);
222-
223204
$productCollection = $this->_productCollectionFactory->create()->setStoreId(
224205
$this->getStoreId()
225206
)->addIdFilter(
@@ -283,4 +264,24 @@ protected function _assignProducts()
283264

284265
return $this;
285266
}
267+
268+
/**
269+
* Find and remove quote items with non existing products
270+
*
271+
* @return void
272+
*/
273+
private function removeItemsWithAbsentProducts()
274+
{
275+
$productCollection = $this->_productCollectionFactory->create()->addIdFilter($this->_productIds);
276+
$existingProductsIds = $productCollection->getAllIds();
277+
$absentProductsIds = array_diff($this->_productIds, $existingProductsIds);
278+
// Remove not existing products from items collection
279+
if (!empty($absentProductsIds)) {
280+
foreach ($absentProductsIds as $productIdToExclude) {
281+
/** @var \Magento\Quote\Model\Quote\Item $quoteItem */
282+
$quoteItem = $this->getItemByColumnValue('product_id', $productIdToExclude);
283+
$this->removeItemByKey($quoteItem->getId());
284+
}
285+
}
286+
}
286287
}

0 commit comments

Comments
 (0)