@@ -141,21 +141,6 @@ public function resetJoinQuotes($quotesTableName, $productId = null)
141
141
return $ this ;
142
142
}
143
143
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
-
159
144
/**
160
145
* After load processing
161
146
*
@@ -165,18 +150,20 @@ protected function _afterLoad()
165
150
{
166
151
parent ::_afterLoad ();
167
152
168
- /**
169
- * Assign parent items
170
- */
153
+ $ productIds = [];
171
154
foreach ($ this as $ item ) {
155
+ // Assign parent items
172
156
if ($ item ->getParentItemId ()) {
173
157
$ item ->setParentItem ($ this ->getItemById ($ item ->getParentItemId ()));
174
158
}
175
159
if ($ this ->_quote ) {
176
160
$ item ->setQuote ($ this ->_quote );
177
161
}
162
+ // Collect quote products ids
163
+ $ productIds [] = (int )$ item ->getProductId ();
178
164
}
179
-
165
+ $ this ->_productIds = array_merge ($ this ->_productIds , $ productIds );
166
+ $ this ->removeItemsWithAbsentProducts ();
180
167
/**
181
168
* Assign options and products
182
169
*/
@@ -214,12 +201,6 @@ protected function _assignOptions()
214
201
protected function _assignProducts ()
215
202
{
216
203
\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
-
223
204
$ productCollection = $ this ->_productCollectionFactory ->create ()->setStoreId (
224
205
$ this ->getStoreId ()
225
206
)->addIdFilter (
@@ -283,4 +264,24 @@ protected function _assignProducts()
283
264
284
265
return $ this ;
285
266
}
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
+ }
286
287
}
0 commit comments