66
77namespace Magento \CatalogSearch \Model \Search \FilterMapper ;
88
9+ use Magento \Framework \App \ObjectManager ;
910use Magento \Framework \App \ResourceConnection ;
1011use Magento \Framework \DB \Select ;
1112use Magento \Framework \Search \Adapter \Mysql \ConditionManager ;
1213use Magento \CatalogInventory \Api \StockConfigurationInterface ;
1314use Magento \CatalogInventory \Api \StockRegistryInterface ;
1415
1516/**
16- * Class StockStatusFilter
1717 * Adds filter by stock status to base select
1818 *
19- * @deprecated
19+ * @deprecated MySQL search engine is not recommended.
2020 * @see \Magento\ElasticSearch
2121 */
2222class StockStatusFilter
@@ -56,23 +56,31 @@ class StockStatusFilter
5656 * @var StockRegistryInterface
5757 */
5858 private $ stockRegistry ;
59+ /**
60+ * @var StockStatusQueryBuilder
61+ */
62+ private $ stockStatusQueryBuilder ;
5963
6064 /**
6165 * @param ResourceConnection $resourceConnection
6266 * @param ConditionManager $conditionManager
6367 * @param StockConfigurationInterface $stockConfiguration
6468 * @param StockRegistryInterface $stockRegistry
69+ * @param StockStatusQueryBuilder|null $stockStatusQueryBuilder
6570 */
6671 public function __construct (
6772 ResourceConnection $ resourceConnection ,
6873 ConditionManager $ conditionManager ,
6974 StockConfigurationInterface $ stockConfiguration ,
70- StockRegistryInterface $ stockRegistry
75+ StockRegistryInterface $ stockRegistry ,
76+ ?StockStatusQueryBuilder $ stockStatusQueryBuilder = null
7177 ) {
7278 $ this ->resourceConnection = $ resourceConnection ;
7379 $ this ->conditionManager = $ conditionManager ;
7480 $ this ->stockConfiguration = $ stockConfiguration ;
7581 $ this ->stockRegistry = $ stockRegistry ;
82+ $ this ->stockStatusQueryBuilder = $ stockStatusQueryBuilder
83+ ?? ObjectManager::getInstance ()->get (StockStatusQueryBuilder::class);
7684 }
7785
7886 /**
@@ -94,99 +102,27 @@ public function apply(Select $select, $stockValues, $type, $showOutOfStockFlag)
94102 $ select = clone $ select ;
95103 $ mainTableAlias = $ this ->extractTableAliasFromSelect ($ select );
96104
97- $ this ->addMainStockStatusJoin ($ select , $ stockValues , $ mainTableAlias , $ showOutOfStockFlag );
105+ $ select = $ this ->stockStatusQueryBuilder ->apply (
106+ $ select ,
107+ $ mainTableAlias ,
108+ 'stock_index ' ,
109+ 'entity_id ' ,
110+ $ showOutOfStockFlag ? null : $ stockValues
111+ );
98112
99113 if ($ type === self ::FILTER_ENTITY_AND_SUB_PRODUCTS ) {
100- $ this ->addSubProductsStockStatusJoin ($ select , $ stockValues , $ mainTableAlias , $ showOutOfStockFlag );
114+ $ select = $ this ->stockStatusQueryBuilder ->apply (
115+ $ select ,
116+ $ mainTableAlias ,
117+ 'sub_products_stock_index ' ,
118+ 'source_id ' ,
119+ $ showOutOfStockFlag ? null : $ stockValues
120+ );
101121 }
102122
103123 return $ select ;
104124 }
105125
106- /**
107- * Adds filter join for products by stock status
108- * In case when $showOutOfStockFlag is true - joins are still required to filter only enabled products
109- *
110- * @param Select $select
111- * @param array|int $stockValues
112- * @param string $mainTableAlias
113- * @param bool $showOutOfStockFlag
114- * @return void
115- */
116- private function addMainStockStatusJoin (Select $ select , $ stockValues , $ mainTableAlias , $ showOutOfStockFlag )
117- {
118- $ catalogInventoryTable = $ this ->resourceConnection ->getTableName ('cataloginventory_stock_status ' );
119- $ select ->joinInner (
120- ['stock_index ' => $ catalogInventoryTable ],
121- $ this ->conditionManager ->combineQueries (
122- [
123- sprintf ('stock_index.product_id = %s.entity_id ' , $ mainTableAlias ),
124- $ this ->conditionManager ->generateCondition (
125- 'stock_index.website_id ' ,
126- '= ' ,
127- $ this ->stockConfiguration ->getDefaultScopeId ()
128- ),
129- $ showOutOfStockFlag
130- ? ''
131- : $ this ->conditionManager ->generateCondition (
132- 'stock_index.stock_status ' ,
133- is_array ($ stockValues ) ? 'in ' : '= ' ,
134- $ stockValues
135- ),
136- $ this ->conditionManager ->generateCondition (
137- 'stock_index.stock_id ' ,
138- '= ' ,
139- (int ) $ this ->stockRegistry ->getStock ()->getStockId ()
140- ),
141- ],
142- Select::SQL_AND
143- ),
144- []
145- );
146- }
147-
148- /**
149- * Adds filter join for sub products by stock status
150- * In case when $showOutOfStockFlag is true - joins are still required to filter only enabled products
151- *
152- * @param Select $select
153- * @param array|int $stockValues
154- * @param string $mainTableAlias
155- * @param bool $showOutOfStockFlag
156- * @return void
157- */
158- private function addSubProductsStockStatusJoin (Select $ select , $ stockValues , $ mainTableAlias , $ showOutOfStockFlag )
159- {
160- $ catalogInventoryTable = $ this ->resourceConnection ->getTableName ('cataloginventory_stock_status ' );
161- $ select ->joinInner (
162- ['sub_products_stock_index ' => $ catalogInventoryTable ],
163- $ this ->conditionManager ->combineQueries (
164- [
165- sprintf ('sub_products_stock_index.product_id = %s.source_id ' , $ mainTableAlias ),
166- $ this ->conditionManager ->generateCondition (
167- 'sub_products_stock_index.website_id ' ,
168- '= ' ,
169- $ this ->stockConfiguration ->getDefaultScopeId ()
170- ),
171- $ showOutOfStockFlag
172- ? ''
173- : $ this ->conditionManager ->generateCondition (
174- 'sub_products_stock_index.stock_status ' ,
175- is_array ($ stockValues ) ? 'in ' : '= ' ,
176- $ stockValues
177- ),
178- $ this ->conditionManager ->generateCondition (
179- 'sub_products_stock_index.stock_id ' ,
180- '= ' ,
181- (int ) $ this ->stockRegistry ->getStock ()->getStockId ()
182- ),
183- ],
184- Select::SQL_AND
185- ),
186- []
187- );
188- }
189-
190126 /**
191127 * Extracts alias for table that is used in FROM clause in Select
192128 *
0 commit comments