6
6
7
7
namespace Magento \CatalogSearch \Model \Search \FilterMapper ;
8
8
9
+ use Magento \Framework \App \ObjectManager ;
9
10
use Magento \Framework \App \ResourceConnection ;
10
11
use Magento \Framework \DB \Select ;
11
12
use Magento \Framework \Search \Adapter \Mysql \ConditionManager ;
12
13
use Magento \CatalogInventory \Api \StockConfigurationInterface ;
13
14
use Magento \CatalogInventory \Api \StockRegistryInterface ;
14
15
15
16
/**
16
- * Class StockStatusFilter
17
17
* Adds filter by stock status to base select
18
18
*
19
- * @deprecated
19
+ * @deprecated MySQL search engine is not recommended.
20
20
* @see \Magento\ElasticSearch
21
21
*/
22
22
class StockStatusFilter
@@ -56,23 +56,31 @@ class StockStatusFilter
56
56
* @var StockRegistryInterface
57
57
*/
58
58
private $ stockRegistry ;
59
+ /**
60
+ * @var StockStatusQueryBuilder
61
+ */
62
+ private $ stockStatusQueryBuilder ;
59
63
60
64
/**
61
65
* @param ResourceConnection $resourceConnection
62
66
* @param ConditionManager $conditionManager
63
67
* @param StockConfigurationInterface $stockConfiguration
64
68
* @param StockRegistryInterface $stockRegistry
69
+ * @param StockStatusQueryBuilder|null $stockStatusQueryBuilder
65
70
*/
66
71
public function __construct (
67
72
ResourceConnection $ resourceConnection ,
68
73
ConditionManager $ conditionManager ,
69
74
StockConfigurationInterface $ stockConfiguration ,
70
- StockRegistryInterface $ stockRegistry
75
+ StockRegistryInterface $ stockRegistry ,
76
+ ?StockStatusQueryBuilder $ stockStatusQueryBuilder = null
71
77
) {
72
78
$ this ->resourceConnection = $ resourceConnection ;
73
79
$ this ->conditionManager = $ conditionManager ;
74
80
$ this ->stockConfiguration = $ stockConfiguration ;
75
81
$ this ->stockRegistry = $ stockRegistry ;
82
+ $ this ->stockStatusQueryBuilder = $ stockStatusQueryBuilder
83
+ ?? ObjectManager::getInstance ()->get (StockStatusQueryBuilder::class);
76
84
}
77
85
78
86
/**
@@ -94,99 +102,27 @@ public function apply(Select $select, $stockValues, $type, $showOutOfStockFlag)
94
102
$ select = clone $ select ;
95
103
$ mainTableAlias = $ this ->extractTableAliasFromSelect ($ select );
96
104
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
+ );
98
112
99
113
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
+ );
101
121
}
102
122
103
123
return $ select ;
104
124
}
105
125
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
-
190
126
/**
191
127
* Extracts alias for table that is used in FROM clause in Select
192
128
*
0 commit comments