@@ -169,18 +169,7 @@ protected function _afterLoad()
169
169
$ productIds [] = (int )$ item ->getProductId ();
170
170
}
171
171
$ 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 ();
184
173
/**
185
174
* Assign options and products
186
175
*/
@@ -218,12 +207,6 @@ protected function _assignOptions()
218
207
protected function _assignProducts ()
219
208
{
220
209
\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
-
227
210
$ productCollection = $ this ->_productCollectionFactory ->create ()->setStoreId (
228
211
$ this ->getStoreId ()
229
212
)->addIdFilter (
@@ -318,4 +301,24 @@ private function addTierPriceData(ProductCollection $productCollection)
318
301
$ productCollection ->addTierPriceDataByGroupId ($ this ->_quote ->getCustomerGroupId ());
319
302
}
320
303
}
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
+ }
321
324
}
0 commit comments