Skip to content

Commit 97c350b

Browse files
committed
MC-37726: Price filter uses base currency for ranges
1 parent 8baf70f commit 97c350b

File tree

2 files changed

+114
-23
lines changed

2 files changed

+114
-23
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Aggregations.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public function resolve(
7474
if (isset($results['price_bucket'])) {
7575
foreach ($results['price_bucket']['options'] as &$value) {
7676
list($from, $to) = explode('-', $value['label']);
77-
$newLabel = $this->priceCurrency->convertAndRound($from) . '-' . $this->priceCurrency->convertAndRound($to);
77+
$newLabel = $this->priceCurrency->convertAndRound($from)
78+
. '-'
79+
. $this->priceCurrency->convertAndRound($to);
7880
$value['label'] = $newLabel;
7981
$value['value'] = str_replace('-', '_', $newLabel);
8082
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductSearchAggregationsTest.php

Lines changed: 111 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
namespace Magento\GraphQl\Catalog;
99

10+
use Magento\Config\App\Config\Type\System;
11+
use Magento\Config\Model\ResourceModel\Config;
12+
use Magento\Directory\Model\Currency;
13+
use Magento\Store\Model\ScopeInterface;
14+
use Magento\Store\Model\Store;
1015
use Magento\TestFramework\Helper\Bootstrap;
1116
use Magento\TestFramework\TestCase\GraphQlAbstract;
1217

@@ -22,28 +27,9 @@ public function testAggregationBooleanAttribute()
2227
. 'MC-36768: Custom attribute not appears in elasticsearch'
2328
);
2429

25-
$skus= '"search_product_1", "search_product_2", "search_product_3", "search_product_4" ,"search_product_5"';
26-
$query = <<<QUERY
27-
{
28-
products(filter: {sku: {in: [{$skus}]}}){
29-
items{
30-
id
31-
sku
32-
name
33-
}
34-
aggregations{
35-
label
36-
attribute_code
37-
count
38-
options{
39-
label
40-
value
41-
count
42-
}
43-
}
44-
}
45-
}
46-
QUERY;
30+
$query = $this->getGraphQlQuery(
31+
'"search_product_1", "search_product_2", "search_product_3", "search_product_4" ,"search_product_5"'
32+
);
4733

4834
$result = $this->graphQlQuery($query);
4935

@@ -69,4 +55,107 @@ function ($a) {
6955
$this->assertCount(2, $booleanAggregation['options']);
7056
$this->assertContainsEquals(['label' => '0', 'value'=> '0', 'count' => '2'], $booleanAggregation['options']);
7157
}
58+
59+
/**
60+
* @magentoApiDataFixture Magento/Catalog/_files/products_for_search.php
61+
*/
62+
public function testAggregationPriceRanges()
63+
{
64+
$query = $this->getGraphQlQuery(
65+
'"search_product_1", "search_product_2", "search_product_3", "search_product_4" ,"search_product_5"'
66+
);
67+
$result = $this->graphQlQuery($query);
68+
69+
$this->assertArrayNotHasKey('errors', $result);
70+
$this->assertArrayHasKey('aggregations', $result['products']);
71+
72+
$priceAggregation = array_filter(
73+
$result['products']['aggregations'],
74+
function ($a) {
75+
return $a['attribute_code'] == 'price';
76+
}
77+
);
78+
$this->assertNotEmpty($priceAggregation);
79+
$priceAggregation = reset($priceAggregation);
80+
$this->assertEquals('Price', $priceAggregation['label']);
81+
$this->assertEquals(4, $priceAggregation['count']);
82+
$expectedOptions = [
83+
['label' => '10-20', 'value'=> '10_20', 'count' => '2'],
84+
['label' => '20-30', 'value'=> '20_30', 'count' => '1'],
85+
['label' => '30-40', 'value'=> '30_40', 'count' => '1'],
86+
['label' => '40-50', 'value'=> '40_50', 'count' => '1']
87+
];
88+
$this->assertEquals($expectedOptions, $priceAggregation['options']);
89+
}
90+
91+
/**
92+
* @magentoApiDataFixture Magento/Store/_files/second_store_with_second_currency.php
93+
* @magentoApiDataFixture Magento/Catalog/_files/products_for_search.php
94+
*/
95+
public function testAggregationPriceRangesWithCurrencyHeader()
96+
{
97+
// add USD as allowed (not default) currency
98+
$objectManager = Bootstrap::getObjectManager();
99+
/* @var Store $store */
100+
$store = $objectManager->create(Store::class);
101+
$store->load('fixture_second_store');
102+
/** @var Config $configResource */
103+
$configResource = $objectManager->get(Config::class);
104+
$configResource->saveConfig(
105+
Currency::XML_PATH_CURRENCY_ALLOW,
106+
'USD',
107+
ScopeInterface::SCOPE_STORES,
108+
$store->getId()
109+
);
110+
// Configuration cache clean is required to reload currency setting
111+
/** @var System $config */
112+
$config = $objectManager->get(System::class);
113+
$config->clean();
114+
115+
$headerMap['Store'] = 'fixture_second_store';
116+
$headerMap['Content-Currency'] = 'USD';
117+
$query = $this->getGraphQlQuery(
118+
'"search_product_1", "search_product_2", "search_product_3", "search_product_4" ,"search_product_5"'
119+
);
120+
$result = $this->graphQlQuery($query, [], '', $headerMap);
121+
$this->assertArrayNotHasKey('errors', $result);
122+
$this->assertArrayHasKey('aggregations', $result['products']);
123+
$priceAggregation = array_filter(
124+
$result['products']['aggregations'],
125+
function ($a) {
126+
return $a['attribute_code'] == 'price';
127+
}
128+
);
129+
$this->assertNotEmpty($priceAggregation);
130+
$priceAggregation = reset($priceAggregation);
131+
$this->assertEquals('Price', $priceAggregation['label']);
132+
$this->assertEquals(4, $priceAggregation['count']);
133+
$expectedOptions = [
134+
['label' => '10-20', 'value'=> '10_20', 'count' => '2'],
135+
['label' => '20-30', 'value'=> '20_30', 'count' => '1'],
136+
['label' => '30-40', 'value'=> '30_40', 'count' => '1'],
137+
['label' => '40-50', 'value'=> '40_50', 'count' => '1']
138+
];
139+
$this->assertEquals($expectedOptions, $priceAggregation['options']);
140+
}
141+
142+
private function getGraphQlQuery(string $skus)
143+
{
144+
return <<<QUERY
145+
{
146+
products(filter: {sku: {in: [{$skus}]}}){
147+
aggregations{
148+
label
149+
attribute_code
150+
count
151+
options{
152+
label
153+
value
154+
count
155+
}
156+
}
157+
}
158+
}
159+
QUERY;
160+
}
72161
}

0 commit comments

Comments
 (0)