6
6
namespace Magento \CatalogSearch \Model \Adapter \Mysql \Filter ;
7
7
8
8
use Magento \Catalog \Model \Product ;
9
+ use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
9
10
use Magento \CatalogSearch \Model \Search \TableMapper ;
10
11
use Magento \Eav \Model \Config ;
11
12
use Magento \Framework \App \ResourceConnection ;
@@ -97,7 +98,7 @@ public function process(FilterInterface $filter, $isNegation, $query)
97
98
*/
98
99
private function processQueryWithField (FilterInterface $ filter , $ isNegation , $ query )
99
100
{
100
- /** @var \Magento\Catalog\Model\ResourceModel\Eav\ Attribute $attribute */
101
+ /** @var Attribute $attribute */
101
102
$ attribute = $ this ->config ->getAttribute (Product::ENTITY , $ filter ->getField ());
102
103
if ($ filter ->getField () === 'price ' ) {
103
104
$ resultQuery = str_replace (
@@ -114,24 +115,16 @@ private function processQueryWithField(FilterInterface $filter, $isNegation, $qu
114
115
$ this ->connection ->quoteIdentifier ($ alias . '. ' . $ attribute ->getAttributeCode ()),
115
116
$ query
116
117
);
117
- } elseif ($ filter ->getType () === FilterInterface::TYPE_TERM
118
- && in_array ($ attribute ->getFrontendInput (), ['select ' , 'multiselect ' ], true )
118
+ } elseif (
119
+ $ filter ->getType () === FilterInterface::TYPE_TERM &&
120
+ in_array ($ attribute ->getFrontendInput (), ['select ' , 'multiselect ' ], true )
119
121
) {
120
- $ alias = $ this ->tableMapper ->getMappingAlias ($ filter );
121
- if (is_array ($ filter ->getValue ())) {
122
- $ value = sprintf (
123
- '%s IN (%s) ' ,
124
- ($ isNegation ? 'NOT ' : '' ),
125
- implode (', ' , $ filter ->getValue ())
126
- );
127
- } else {
128
- $ value = ($ isNegation ? '! ' : '' ) . '= ' . $ filter ->getValue ();
129
- }
130
- $ resultQuery = sprintf (
131
- '%1$s.value %2$s ' ,
132
- $ alias ,
133
- $ value
134
- );
122
+ $ resultQuery = $ this ->processTermSelect ($ filter , $ isNegation );
123
+ } elseif (
124
+ $ filter ->getType () === FilterInterface::TYPE_RANGE &&
125
+ in_array ($ attribute ->getBackendType (), ['decimal ' , 'int ' ], true )
126
+ ) {
127
+ $ resultQuery = $ this ->processRangeNumeric ($ filter , $ query , $ attribute );
135
128
} else {
136
129
$ table = $ attribute ->getBackendTable ();
137
130
$ select = $ this ->connection ->select ();
@@ -161,4 +154,57 @@ private function processQueryWithField(FilterInterface $filter, $isNegation, $qu
161
154
162
155
return $ resultQuery ;
163
156
}
157
+
158
+ /**
159
+ * @param FilterInterface $filter
160
+ * @param string $query
161
+ * @param Attribute $attribute
162
+ * @return string
163
+ */
164
+ private function processRangeNumeric (FilterInterface $ filter , $ query , $ attribute )
165
+ {
166
+ $ tableSuffix = $ attribute ->getBackendType () === 'decimal ' ? '_decimal ' : '' ;
167
+ $ table = $ this ->resource ->getTableName ("catalog_product_index_eav {$ tableSuffix }" );
168
+ $ select = $ this ->connection ->select ();
169
+
170
+ $ currentStoreId = $ this ->scopeResolver ->getScope ()->getId ();
171
+
172
+ $ select ->from (['main_table ' => $ table ], 'entity_id ' )
173
+ ->columns ([$ filter ->getField () => 'main_table.value ' ])
174
+ ->where ('main_table.attribute_id = ? ' , $ attribute ->getAttributeId ())
175
+ ->where ('main_table.store_id = ? ' , $ currentStoreId )
176
+ ->having ($ query );
177
+
178
+ $ resultQuery = 'search_index.entity_id IN (
179
+ select entity_id from ' . $ this ->conditionManager ->wrapBrackets ($ select ) . ' as filter
180
+ ) ' ;
181
+
182
+ return $ resultQuery ;
183
+ }
184
+
185
+ /**
186
+ * @param FilterInterface $filter
187
+ * @param bool $isNegation
188
+ * @return string
189
+ */
190
+ private function processTermSelect (FilterInterface $ filter , $ isNegation )
191
+ {
192
+ $ alias = $ this ->tableMapper ->getMappingAlias ($ filter );
193
+ if (is_array ($ filter ->getValue ())) {
194
+ $ value = sprintf (
195
+ '%s IN (%s) ' ,
196
+ ($ isNegation ? 'NOT ' : '' ),
197
+ implode (', ' , $ filter ->getValue ())
198
+ );
199
+ } else {
200
+ $ value = ($ isNegation ? '! ' : '' ) . '= ' . $ filter ->getValue ();
201
+ }
202
+ $ resultQuery = sprintf (
203
+ '%1$s.value %2$s ' ,
204
+ $ alias ,
205
+ $ value
206
+ );
207
+
208
+ return $ resultQuery ;
209
+ }
164
210
}
0 commit comments