6
6
namespace Magento \Wishlist \Model \ResourceModel \Item ;
7
7
8
8
use Magento \Catalog \Api \Data \ProductInterface ;
9
+ use Magento \Catalog \Model \Indexer \Category \Product \TableMaintainer ;
10
+ use Magento \CatalogInventory \Model \Stock ;
11
+ use Magento \Framework \App \ObjectManager ;
9
12
use Magento \Framework \EntityManager \MetadataPool ;
13
+ use Magento \Sales \Model \Config ;
10
14
11
15
/**
12
16
* Wishlist item collection
@@ -144,6 +148,16 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
144
148
*/
145
149
protected $ metadataPool ;
146
150
151
+ /**
152
+ * @var TableMaintainer
153
+ */
154
+ private $ tableMaintainer ;
155
+
156
+ /**
157
+ * @var Config
158
+ */
159
+ private $ salesConfig ;
160
+
147
161
/**
148
162
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
149
163
* @param \Psr\Log\LoggerInterface $logger
@@ -163,6 +177,8 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
163
177
* @param \Magento\Wishlist\Model\ResourceModel\Item $resource
164
178
* @param \Magento\Framework\App\State $appState
165
179
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
180
+ * @param TableMaintainer|null $tableMaintainer
181
+ * @param Config|null $salesConfig
166
182
*
167
183
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
168
184
*/
@@ -184,7 +200,9 @@ public function __construct(
184
200
\Magento \Catalog \Model \Entity \AttributeFactory $ catalogAttrFactory ,
185
201
\Magento \Wishlist \Model \ResourceModel \Item $ resource ,
186
202
\Magento \Framework \App \State $ appState ,
187
- \Magento \Framework \DB \Adapter \AdapterInterface $ connection = null
203
+ \Magento \Framework \DB \Adapter \AdapterInterface $ connection = null ,
204
+ TableMaintainer $ tableMaintainer = null ,
205
+ Config $ salesConfig = null
188
206
) {
189
207
$ this ->stockConfiguration = $ stockConfiguration ;
190
208
$ this ->_adminhtmlSales = $ adminhtmlSales ;
@@ -199,6 +217,8 @@ public function __construct(
199
217
$ this ->_catalogAttrFactory = $ catalogAttrFactory ;
200
218
$ this ->_appState = $ appState ;
201
219
parent ::__construct ($ entityFactory , $ logger , $ fetchStrategy , $ eventManager , $ connection , $ resource );
220
+ $ this ->tableMaintainer = $ tableMaintainer ?: ObjectManager::getInstance ()->get (TableMaintainer::class);
221
+ $ this ->salesConfig = $ salesConfig ?: ObjectManager::getInstance ()->get (Config::class);
202
222
}
203
223
204
224
/**
@@ -330,6 +350,53 @@ protected function _assignProducts()
330
350
return $ this ;
331
351
}
332
352
353
+ /**
354
+ * @inheritdoc
355
+ */
356
+ protected function _renderFiltersBefore ()
357
+ {
358
+ parent ::_renderFiltersBefore ();
359
+
360
+ $ mainTableName = 'main_table ' ;
361
+ $ connection = $ this ->getConnection ();
362
+
363
+ if ($ this ->_productInStock && !$ this ->stockConfiguration ->isShowOutOfStock ()) {
364
+ $ inStockConditions = [
365
+ "stockItem.product_id = {$ mainTableName }.product_id " ,
366
+ $ connection ->quoteInto ('stockItem.stock_status = ? ' , Stock::STOCK_IN_STOCK ),
367
+ ];
368
+ $ this ->getSelect ()->join (
369
+ ['stockItem ' => $ this ->getTable ('cataloginventory_stock_status ' )],
370
+ join (' AND ' , $ inStockConditions ),
371
+ []
372
+ );
373
+ }
374
+
375
+ if ($ this ->_productVisible ) {
376
+ $ rootCategoryId = $ this ->_storeManager ->getStore ()->getRootCategoryId ();
377
+ $ visibleInSiteIds = $ this ->_productVisibility ->getVisibleInSiteIds ();
378
+ $ visibilityConditions = [
379
+ "cat_index.product_id = {$ mainTableName }.product_id " ,
380
+ $ connection ->quoteInto ('cat_index.category_id = ? ' , $ rootCategoryId ),
381
+ $ connection ->quoteInto ('cat_index.visibility IN (?) ' , $ visibleInSiteIds )
382
+ ];
383
+ $ this ->getSelect ()->join (
384
+ ['cat_index ' => $ this ->tableMaintainer ->getMainTable ($ this ->_storeManager ->getStore ()->getId ())],
385
+ join (' AND ' , $ visibilityConditions ),
386
+ []
387
+ );
388
+ }
389
+
390
+ if ($ this ->_productSalable ) {
391
+ $ availableProductTypes = $ this ->salesConfig ->getAvailableProductTypes ();
392
+ $ this ->getSelect ()->join (
393
+ ['cat_prod ' => $ this ->getTable ('catalog_product_entity ' )],
394
+ $ this ->getConnection ()->quoteInto ('cat_prod.type_id IN (?) ' , $ availableProductTypes ),
395
+ []
396
+ );
397
+ }
398
+ }
399
+
333
400
/**
334
401
* Add filter by wishlist object
335
402
*
0 commit comments