Skip to content

Commit

Permalink
ENGCOM-7707: 28628 price range wildcards #28745
Browse files Browse the repository at this point in the history
  • Loading branch information
cpartica committed Jun 27, 2020
2 parents 46450bf + 6f647d1 commit 3786d5a
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 56 deletions.
8 changes: 4 additions & 4 deletions app/code/Magento/Catalog/Model/Layer/Filter/Price/Render.php
Expand Up @@ -72,6 +72,8 @@ public function renderRangeLabel($fromPrice, $toPrice)
}

/**
* Prepare range data
*
* @param int $range
* @param int[] $dbRanges
* @return array
Expand All @@ -81,12 +83,10 @@ public function renderRangeData($range, $dbRanges)
if (empty($dbRanges)) {
return [];
}
$lastIndex = array_keys($dbRanges);
$lastIndex = $lastIndex[count($lastIndex) - 1];

foreach ($dbRanges as $index => $count) {
$fromPrice = $index == 1 ? '' : ($index - 1) * $range;
$toPrice = $index == $lastIndex ? '' : $index * $range;
$fromPrice = $index == 1 ? 0 : ($index - 1) * $range;
$toPrice = $index * $range;
$this->itemDataBuilder->addItemData(
$this->renderRangeLabel($fromPrice, $toPrice),
$fromPrice . '-' . $toPrice,
Expand Down
23 changes: 11 additions & 12 deletions app/code/Magento/CatalogSearch/Model/Layer/Filter/Price.php
Expand Up @@ -176,15 +176,16 @@ public function getCurrencyRate()
*
* @param float|string $fromPrice
* @param float|string $toPrice
* @param boolean $isLast
* @return float|\Magento\Framework\Phrase
*/
protected function _renderRangeLabel($fromPrice, $toPrice)
protected function _renderRangeLabel($fromPrice, $toPrice, $isLast = false)
{
$fromPrice = empty($fromPrice) ? 0 : $fromPrice * $this->getCurrencyRate();
$toPrice = empty($toPrice) ? $toPrice : $toPrice * $this->getCurrencyRate();

$formattedFromPrice = $this->priceCurrency->format($fromPrice);
if ($toPrice === '') {
if ($isLast) {
return __('%1 and above', $formattedFromPrice);
} elseif ($fromPrice == $toPrice && $this->dataProvider->getOnePriceIntervalValue()) {
return $formattedFromPrice;
Expand Down Expand Up @@ -215,12 +216,15 @@ protected function _getItemsData()

$data = [];
if (count($facets) > 1) { // two range minimum
$lastFacet = array_key_last($facets);
foreach ($facets as $key => $aggregation) {
$count = $aggregation['count'];
if (strpos($key, '_') === false) {
continue;
}
$data[] = $this->prepareData($key, $count, $data);

$isLast = $lastFacet === $key;
$data[] = $this->prepareData($key, $count, $isLast);
}
}

Expand Down Expand Up @@ -264,18 +268,13 @@ protected function getFrom($from)
*
* @param string $key
* @param int $count
* @param boolean $isLast
* @return array
*/
private function prepareData($key, $count)
private function prepareData($key, $count, $isLast = false)
{
list($from, $to) = explode('_', $key);
if ($from == '*') {
$from = $this->getFrom($to);
}
if ($to == '*') {
$to = $this->getTo($to);
}
$label = $this->_renderRangeLabel($from, $to);
[$from, $to] = explode('_', $key);
$label = $this->_renderRangeLabel($from, $to, $isLast);
$value = $from . '-' . $to . $this->dataProvider->getAdditionalRequestData();

$data = [
Expand Down
Expand Up @@ -35,7 +35,7 @@ public function __construct(Repository $algorithmRepository, EntityStorageFactor
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function build(
RequestBucketInterface $bucket,
Expand All @@ -46,9 +46,7 @@ public function build(
/** @var DynamicBucket $bucket */
$algorithm = $this->algorithmRepository->get($bucket->getMethod(), ['dataProvider' => $dataProvider]);
$data = $algorithm->getItems($bucket, $dimensions, $this->getEntityStorage($queryResult));
$resultData = $this->prepareData($data);

return $resultData;
return $this->prepareData($data);
}

/**
Expand Down Expand Up @@ -77,12 +75,9 @@ private function prepareData($data)
{
$resultData = [];
foreach ($data as $value) {
$from = is_numeric($value['from']) ? $value['from'] : '*';
$to = is_numeric($value['to']) ? $value['to'] : '*';
unset($value['from'], $value['to']);

$rangeName = "{$from}_{$to}";
$resultData[$rangeName] = array_merge(['value' => $rangeName], $value);
$rangeName = "{$value['from']}_{$value['to']}";
$value['value'] = $rangeName;
$resultData[$rangeName] = $value;
}

return $resultData;
Expand Down
Expand Up @@ -235,11 +235,9 @@ public function prepareData($range, array $dbRanges)
{
$data = [];
if (!empty($dbRanges)) {
$lastIndex = array_keys($dbRanges);
$lastIndex = $lastIndex[count($lastIndex) - 1];
foreach ($dbRanges as $index => $count) {
$fromPrice = $index == 1 ? '' : ($index - 1) * $range;
$toPrice = $index == $lastIndex ? '' : $index * $range;
$fromPrice = $index == 1 ? 0 : ($index - 1) * $range;
$toPrice = $index * $range;
$data[] = [
'from' => $fromPrice,
'to' => $toPrice,
Expand Down
Expand Up @@ -390,13 +390,13 @@ public function testPrepareData()
{
$expectedResult = [
[
'from' => '',
'from' => 0,
'to' => 10,
'count' => 1,
],
[
'from' => 10,
'to' => '',
'to' => 20,
'count' => 1,
],
];
Expand Down
Expand Up @@ -570,8 +570,8 @@ public function testSearchAndFilterByCustomAttribute()
],
[
'count' => 1,
'label' => '40-*',
'value' => '40_*',
'label' => '40-50',
'value' => '40_50',

],
],
Expand Down Expand Up @@ -1431,8 +1431,8 @@ public function testFilterProductsForExactMatchingName()
'count' => 1,
],
[
'label' => '20-*',
'value' => '20_*',
'label' => '20-30',
'value' => '20_30',
'count' => 1,
]
]
Expand Down
Expand Up @@ -53,7 +53,7 @@ public function testGetFilters(): void
['is_filterable' => '1'],
[
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-30', 'count' => 1],
],
'Category 1'
);
Expand Down
Expand Up @@ -76,7 +76,7 @@ public function getFiltersDataProvider(): array
],
[
'label' => '<span class="price">$60.00</span> and above',
'value' => '60-',
'value' => '60-70',
'count' => 1,
],
],
Expand All @@ -94,7 +94,7 @@ public function getFiltersDataProvider(): array
],
[
'label' => '<span class="price">$50.00</span> and above',
'value' => '50-',
'value' => '50-60',
'count' => 1,
],
],
Expand Down
Expand Up @@ -71,15 +71,15 @@ public function getFiltersDataProvider(): array
'expectation' => [
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 - $29.99', 'value' => '20-30', 'count' => 1],
['label' => '$50.00 and above', 'value' => '50-', 'count' => 1],
['label' => '$50.00 and above', 'value' => '50-60', 'count' => 1],
],
],
'auto_calculation_variation_with_big_price_difference' => [
'config' => ['catalog/layered_navigation/price_range_calculation' => 'auto'],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 300.00],
'expectation' => [
['label' => '$0.00 - $99.99', 'value' => '-100', 'count' => 2],
['label' => '$300.00 and above', 'value' => '300-', 'count' => 1],
['label' => '$0.00 - $99.99', 'value' => '0-100', 'count' => 2],
['label' => '$300.00 and above', 'value' => '300-400', 'count' => 1],
],
],
'auto_calculation_variation_with_fixed_price_step' => [
Expand All @@ -88,7 +88,7 @@ public function getFiltersDataProvider(): array
'expectation' => [
['label' => '$300.00 - $399.99', 'value' => '300-400', 'count' => 1],
['label' => '$400.00 - $499.99', 'value' => '400-500', 'count' => 1],
['label' => '$500.00 and above', 'value' => '500-', 'count' => 1],
['label' => '$500.00 and above', 'value' => '500-600', 'count' => 1],
],
],
'improved_calculation_variation_with_small_price_difference' => [
Expand All @@ -98,8 +98,8 @@ public function getFiltersDataProvider(): array
],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 50.00],
'expectation' => [
['label' => '$0.00 - $49.99', 'value' => '-50', 'count' => 2],
['label' => '$50.00 and above', 'value' => '50-', 'count' => 1],
['label' => '$0.00 - $19.99', 'value' => '0-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-50', 'count' => 2],
],
],
'improved_calculation_variation_with_big_price_difference' => [
Expand All @@ -109,8 +109,8 @@ public function getFiltersDataProvider(): array
],
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 300.00],
'expectation' => [
['label' => '$0.00 - $299.99', 'value' => '-300', 'count' => 2.0],
['label' => '$300.00 and above', 'value' => '300-', 'count' => 1.0],
['label' => '$0.00 - $19.99', 'value' => '0-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-300', 'count' => 2],
],
],
'manual_calculation_with_price_step_200' => [
Expand All @@ -121,7 +121,7 @@ public function getFiltersDataProvider(): array
'products_data' => ['simple1000' => 300.00, 'simple1001' => 300.00, 'simple1002' => 500.00],
'expectation' => [
['label' => '$200.00 - $399.99', 'value' => '200-400', 'count' => 2],
['label' => '$400.00 and above', 'value' => '400-', 'count' => 1],
['label' => '$400.00 and above', 'value' => '400-600', 'count' => 1],
],
],
'manual_calculation_with_price_step_10' => [
Expand All @@ -132,7 +132,7 @@ public function getFiltersDataProvider(): array
'products_data' => ['simple1000' => 300.00, 'simple1001' => 300.00, 'simple1002' => 500.00],
'expectation' => [
['label' => '$300.00 - $309.99', 'value' => '300-310', 'count' => 2],
['label' => '$500.00 and above', 'value' => '500-', 'count' => 1],
['label' => '$500.00 and above', 'value' => '500-510', 'count' => 1],
],
],
'manual_calculation_with_number_of_intervals_10' => [
Expand All @@ -145,7 +145,7 @@ public function getFiltersDataProvider(): array
'expectation' => [
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 - $29.99', 'value' => '20-30', 'count' => 1],
['label' => '$30.00 and above', 'value' => '30-', 'count' => 1],
['label' => '$30.00 and above', 'value' => '30-40', 'count' => 1],
],
],
'manual_calculation_with_number_of_intervals_2' => [
Expand All @@ -157,7 +157,7 @@ public function getFiltersDataProvider(): array
'products_data' => ['simple1000' => 10.00, 'simple1001' => 20.00, 'simple1002' => 30.00],
'expectation' => [
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-', 'count' => 2],
['label' => '$20.00 and above', 'value' => '20-30', 'count' => 2],
],
],
];
Expand Down
Expand Up @@ -32,7 +32,7 @@ public function testGetFilters(): void
['is_filterable_in_search' => 1],
[
['label' => '$10.00 - $19.99', 'value' => '10-20', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-', 'count' => 1],
['label' => '$20.00 and above', 'value' => '20-30', 'count' => 1],
]
);
}
Expand Down
Expand Up @@ -44,7 +44,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getItems(
BucketInterface $bucket,
Expand All @@ -64,13 +64,12 @@ public function getItems(
$aggregations['count']
);

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

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

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

$dataSize = count($data);
for ($key = 0; $key < $dataSize; $key++) {
Expand Down

0 comments on commit 3786d5a

Please sign in to comment.