Skip to content

Commit 256806a

Browse files
committed
#28628: GraphQL price range numeric values
- Fixed price range wildcard values and associated test cases
1 parent fbd10ba commit 256806a

File tree

6 files changed

+30
-38
lines changed

6 files changed

+30
-38
lines changed

app/code/Magento/Catalog/Model/Layer/Filter/Price/Render.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,10 @@ public function renderRangeData($range, $dbRanges)
8181
if (empty($dbRanges)) {
8282
return [];
8383
}
84-
$lastIndex = array_keys($dbRanges);
85-
$lastIndex = $lastIndex[count($lastIndex) - 1];
8684

8785
foreach ($dbRanges as $index => $count) {
88-
$fromPrice = $index == 1 ? '' : ($index - 1) * $range;
89-
$toPrice = $index == $lastIndex ? '' : $index * $range;
86+
$fromPrice = $index == 1 ? 0 : ($index - 1) * $range;
87+
$toPrice = $index * $range;
9088
$this->itemDataBuilder->addItemData(
9189
$this->renderRangeLabel($fromPrice, $toPrice),
9290
$fromPrice . '-' . $toPrice,

app/code/Magento/CatalogSearch/Model/Layer/Filter/Price.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function apply(\Magento\Framework\App\RequestInterface $request)
138138
$this->dataProvider->setPriorIntervals($priorFilters);
139139
}
140140

141-
list($from, $to) = $filter;
141+
[$from, $to] = $filter;
142142

143143
$this->getLayer()->getProductCollection()->addFieldToFilter(
144144
'price',
@@ -176,15 +176,16 @@ public function getCurrencyRate()
176176
*
177177
* @param float|string $fromPrice
178178
* @param float|string $toPrice
179+
* @param boolean $isLast
179180
* @return float|\Magento\Framework\Phrase
180181
*/
181-
protected function _renderRangeLabel($fromPrice, $toPrice)
182+
protected function _renderRangeLabel($fromPrice, $toPrice, $isLast = false)
182183
{
183184
$fromPrice = empty($fromPrice) ? 0 : $fromPrice * $this->getCurrencyRate();
184185
$toPrice = empty($toPrice) ? $toPrice : $toPrice * $this->getCurrencyRate();
185186

186187
$formattedFromPrice = $this->priceCurrency->format($fromPrice);
187-
if ($toPrice === '') {
188+
if ($isLast) {
188189
return __('%1 and above', $formattedFromPrice);
189190
} elseif ($fromPrice == $toPrice && $this->dataProvider->getOnePriceIntervalValue()) {
190191
return $formattedFromPrice;
@@ -215,12 +216,15 @@ protected function _getItemsData()
215216

216217
$data = [];
217218
if (count($facets) > 1) { // two range minimum
219+
$lastFacet = array_key_last($facets);
218220
foreach ($facets as $key => $aggregation) {
219221
$count = $aggregation['count'];
220222
if (strpos($key, '_') === false) {
221223
continue;
222224
}
223-
$data[] = $this->prepareData($key, $count, $data);
225+
226+
$isLast = $lastFacet === $key;
227+
$data[] = $this->prepareData($key, $count, $isLast);
224228
}
225229
}
226230

@@ -264,18 +268,13 @@ protected function getFrom($from)
264268
*
265269
* @param string $key
266270
* @param int $count
271+
* @param boolean $isLast
267272
* @return array
268273
*/
269-
private function prepareData($key, $count)
274+
private function prepareData($key, $count, $isLast = false)
270275
{
271-
list($from, $to) = explode('_', $key);
272-
if ($from == '*') {
273-
$from = $this->getFrom($to);
274-
}
275-
if ($to == '*') {
276-
$to = $this->getTo($to);
277-
}
278-
$label = $this->_renderRangeLabel($from, $to);
276+
[$from, $to] = explode('_', $key);
277+
$label = $this->_renderRangeLabel($from, $to, $isLast);
279278
$value = $from . '-' . $to . $this->dataProvider->getAdditionalRequestData();
280279

281280
$data = [

app/code/Magento/Elasticsearch/SearchAdapter/Aggregation/Builder/Dynamic.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ private function prepareData($data)
7777
{
7878
$resultData = [];
7979
foreach ($data as $value) {
80-
$from = is_numeric($value['from']) ? $value['from'] : '*';
81-
$to = is_numeric($value['to']) ? $value['to'] : '*';
82-
unset($value['from'], $value['to']);
83-
84-
$rangeName = "{$from}_{$to}";
80+
$rangeName = "{$value['from']}_{$value['to']}";
8581
$resultData[$rangeName] = array_merge(['value' => $rangeName], $value);
8682
}
8783

dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/Category/Bundle/PriceFilterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testGetFilters(): void
5353
['is_filterable' => '1'],
5454
[
5555
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
56-
['label' => '$20.00 and above', 'value' => '20-', 'count' => 1],
56+
['label' => '$20.00 and above', 'value' => '20-30', 'count' => 1],
5757
],
5858
'Category 1'
5959
);

dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/Category/PriceFilterTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ public function getFiltersDataProvider(): array
7171
'expectation' => [
7272
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
7373
['label' => '$20.00 - $29.99', 'value' => '20-30', 'count' => 1],
74-
['label' => '$50.00 and above', 'value' => '50-', 'count' => 1],
74+
['label' => '$50.00 and above', 'value' => '50-60', 'count' => 1],
7575
],
7676
],
7777
'auto_calculation_variation_with_big_price_difference' => [
7878
'config' => ['catalog/layered_navigation/price_range_calculation' => 'auto'],
7979
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 300.00],
8080
'expectation' => [
81-
['label' => '$0.00 - $99.99', 'value' => '-100', 'count' => 2],
82-
['label' => '$300.00 and above', 'value' => '300-', 'count' => 1],
81+
['label' => '$0.00 - $99.99', 'value' => '0-100', 'count' => 2],
82+
['label' => '$300.00 and above', 'value' => '300-400', 'count' => 1],
8383
],
8484
],
8585
'auto_calculation_variation_with_fixed_price_step' => [
@@ -88,7 +88,7 @@ public function getFiltersDataProvider(): array
8888
'expectation' => [
8989
['label' => '$300.00 - $399.99', 'value' => '300-400', 'count' => 1],
9090
['label' => '$400.00 - $499.99', 'value' => '400-500', 'count' => 1],
91-
['label' => '$500.00 and above', 'value' => '500-', 'count' => 1],
91+
['label' => '$500.00 and above', 'value' => '500-600', 'count' => 1],
9292
],
9393
],
9494
'improved_calculation_variation_with_small_price_difference' => [
@@ -98,8 +98,8 @@ public function getFiltersDataProvider(): array
9898
],
9999
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 50.00],
100100
'expectation' => [
101-
['label' => '$0.00 - $49.99', 'value' => '-50', 'count' => 2],
102-
['label' => '$50.00 and above', 'value' => '50-', 'count' => 1],
101+
['label' => '$0.00 - $19.99', 'value' => '0-20', 'count' => 1],
102+
['label' => '$20.00 and above', 'value' => '20-50', 'count' => 2],
103103
],
104104
],
105105
'improved_calculation_variation_with_big_price_difference' => [
@@ -109,8 +109,8 @@ public function getFiltersDataProvider(): array
109109
],
110110
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 300.00],
111111
'expectation' => [
112-
['label' => '$0.00 - $299.99', 'value' => '-300', 'count' => 2.0],
113-
['label' => '$300.00 and above', 'value' => '300-', 'count' => 1.0],
112+
['label' => '$0.00 - $19.99', 'value' => '0-20', 'count' => 1],
113+
['label' => '$20.00 and above', 'value' => '20-300', 'count' => 2],
114114
],
115115
],
116116
'manual_calculation_with_price_step_200' => [
@@ -121,7 +121,7 @@ public function getFiltersDataProvider(): array
121121
'products_data' => ['simple1000' => 300.00, 'simple1001' => 300.00, 'simple1002' => 500.00],
122122
'expectation' => [
123123
['label' => '$200.00 - $399.99', 'value' => '200-400', 'count' => 2],
124-
['label' => '$400.00 and above', 'value' => '400-', 'count' => 1],
124+
['label' => '$400.00 and above', 'value' => '400-600', 'count' => 1],
125125
],
126126
],
127127
'manual_calculation_with_price_step_10' => [
@@ -132,7 +132,7 @@ public function getFiltersDataProvider(): array
132132
'products_data' => ['simple1000' => 300.00, 'simple1001' => 300.00, 'simple1002' => 500.00],
133133
'expectation' => [
134134
['label' => '$300.00 - $309.99', 'value' => '300-310', 'count' => 2],
135-
['label' => '$500.00 and above', 'value' => '500-', 'count' => 1],
135+
['label' => '$500.00 and above', 'value' => '500-510', 'count' => 1],
136136
],
137137
],
138138
'manual_calculation_with_number_of_intervals_10' => [
@@ -145,7 +145,7 @@ public function getFiltersDataProvider(): array
145145
'expectation' => [
146146
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
147147
['label' => '$20.00 - $29.99', 'value' => '20-30', 'count' => 1],
148-
['label' => '$30.00 and above', 'value' => '30-', 'count' => 1],
148+
['label' => '$30.00 and above', 'value' => '30-40', 'count' => 1],
149149
],
150150
],
151151
'manual_calculation_with_number_of_intervals_2' => [
@@ -157,7 +157,7 @@ public function getFiltersDataProvider(): array
157157
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 30.00],
158158
'expectation' => [
159159
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
160-
['label' => '$20.00 and above', 'value' => '20-', 'count' => 2],
160+
['label' => '$20.00 and above', 'value' => '20-30', 'count' => 2],
161161
],
162162
],
163163
];

lib/internal/Magento/Framework/Search/Dynamic/Algorithm/Improved.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ public function getItems(
6464
$aggregations['count']
6565
);
6666

67-
$this->algorithm->setLimits($aggregations['min'], $aggregations['max'] + 0.01);
67+
$this->algorithm->setLimits($aggregations['min'], $aggregations['max']);
6868

6969
$interval = $this->dataProvider->getInterval($bucket, $dimensions, $entityStorage);
7070
$data = $this->algorithm->calculateSeparators($interval);
7171

72-
$data[0]['from'] = ''; // We should not calculate min and max value
73-
$data[count($data) - 1]['to'] = '';
72+
$data[0]['from'] = 0;
7473

7574
$dataSize = count($data);
7675
for ($key = 0; $key < $dataSize; $key++) {

0 commit comments

Comments
 (0)