Skip to content

Commit f34eb55

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-44118' into PR
2 parents bd1e52a + c5e4f55 commit f34eb55

File tree

1 file changed

+64
-18
lines changed

1 file changed

+64
-18
lines changed

app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Filter/Preprocessor.php

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CatalogSearch\Model\Adapter\Mysql\Filter;
77

88
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
910
use Magento\CatalogSearch\Model\Search\TableMapper;
1011
use Magento\Eav\Model\Config;
1112
use Magento\Framework\App\ResourceConnection;
@@ -97,7 +98,7 @@ public function process(FilterInterface $filter, $isNegation, $query)
9798
*/
9899
private function processQueryWithField(FilterInterface $filter, $isNegation, $query)
99100
{
100-
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
101+
/** @var Attribute $attribute */
101102
$attribute = $this->config->getAttribute(Product::ENTITY, $filter->getField());
102103
if ($filter->getField() === 'price') {
103104
$resultQuery = str_replace(
@@ -114,24 +115,16 @@ private function processQueryWithField(FilterInterface $filter, $isNegation, $qu
114115
$this->connection->quoteIdentifier($alias . '.' . $attribute->getAttributeCode()),
115116
$query
116117
);
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)
119121
) {
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);
135128
} else {
136129
$table = $attribute->getBackendTable();
137130
$select = $this->connection->select();
@@ -161,4 +154,57 @@ private function processQueryWithField(FilterInterface $filter, $isNegation, $qu
161154

162155
return $resultQuery;
163156
}
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+
}
164210
}

0 commit comments

Comments
 (0)