66namespace Magento \Wishlist \Model \ResourceModel \Item ;
77
88use 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 ;
912use Magento \Framework \EntityManager \MetadataPool ;
13+ use Magento \Sales \Model \ConfigInterface ;
1014
1115/**
1216 * Wishlist item collection
@@ -144,6 +148,16 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
144148 */
145149 protected $ metadataPool ;
146150
151+ /**
152+ * @var TableMaintainer
153+ */
154+ private $ tableMaintainer ;
155+
156+ /**
157+ * @var ConfigInterface
158+ */
159+ private $ salesConfig ;
160+
147161 /**
148162 * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
149163 * @param \Psr\Log\LoggerInterface $logger
@@ -163,6 +177,8 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
163177 * @param \Magento\Wishlist\Model\ResourceModel\Item $resource
164178 * @param \Magento\Framework\App\State $appState
165179 * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
180+ * @param TableMaintainer|null $tableMaintainer
181+ * @param ConfigInterface|null $salesConfig
166182 *
167183 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
168184 */
@@ -184,7 +200,9 @@ public function __construct(
184200 \Magento \Catalog \Model \Entity \AttributeFactory $ catalogAttrFactory ,
185201 \Magento \Wishlist \Model \ResourceModel \Item $ resource ,
186202 \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+ ConfigInterface $ salesConfig = null
188206 ) {
189207 $ this ->stockConfiguration = $ stockConfiguration ;
190208 $ this ->_adminhtmlSales = $ adminhtmlSales ;
@@ -199,6 +217,8 @@ public function __construct(
199217 $ this ->_catalogAttrFactory = $ catalogAttrFactory ;
200218 $ this ->_appState = $ appState ;
201219 parent ::__construct ($ entityFactory , $ logger , $ fetchStrategy , $ eventManager , $ connection , $ resource );
220+ $ this ->tableMaintainer = $ tableMaintainer ?: ObjectManager::getInstance ()->get (TableMaintainer::class);
221+ $ this ->salesConfig = $ salesConfig ?: ObjectManager::getInstance ()->get (ConfigInterface::class);
202222 }
203223
204224 /**
@@ -330,6 +350,53 @@ protected function _assignProducts()
330350 return $ this ;
331351 }
332352
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+
333400 /**
334401 * Add filter by wishlist object
335402 *
0 commit comments