Skip to content

Commit 09be7f8

Browse files
committed
#AC-7920:Not getting correct label value of yes or no (Boolean) type attribute in aggregations data of products GraphQl response-fixed label by adding Yes/No on boolean
1 parent cd41bc2 commit 09be7f8

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

app/code/Magento/CatalogGraphQl/DataProvider/Product/LayeredNavigation/AttributeOptionProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function getOptions(array $optionIds, ?int $storeId, array $attributeCode
6363
'attribute_id' => 'a.attribute_id',
6464
'attribute_code' => 'a.attribute_code',
6565
'attribute_label' => 'a.frontend_label',
66+
'attribute_type' => 'a.frontend_input',
6667
'position' => 'attribute_configuration.position'
6768
]
6869
)
@@ -137,6 +138,7 @@ private function formatResult(Select $select): array
137138
'attribute_code' => $option['attribute_code'],
138139
'attribute_label' => $option['attribute_store_label']
139140
? $option['attribute_store_label'] : $option['attribute_label'],
141+
'attribute_type' => $option['attribute_type'],
140142
'position' => $option['position'],
141143
'options' => [],
142144
];

app/code/Magento/CatalogGraphQl/DataProvider/Product/LayeredNavigation/Builder/Attribute.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Api\Search\AggregationValueInterface;
1414
use Magento\Framework\Api\Search\BucketInterface;
1515
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Formatter\LayerFormatter;
16+
use Magento\Config\Model\Config\Source\Yesno;
1617

1718
/**
1819
* @inheritdoc
@@ -49,18 +50,26 @@ class Attribute implements LayerBuilderInterface
4950
self::CATEGORY_BUCKET
5051
];
5152

53+
/**
54+
* @var Yesno
55+
*/
56+
private Yesno $YesNo;
57+
5258
/**
5359
* @param AttributeOptionProvider $attributeOptionProvider
5460
* @param LayerFormatter $layerFormatter
61+
* @param Yesno $YesNo
5562
* @param array $bucketNameFilter
5663
*/
5764
public function __construct(
5865
AttributeOptionProvider $attributeOptionProvider,
5966
LayerFormatter $layerFormatter,
67+
Yesno $YesNo,
6068
$bucketNameFilter = []
6169
) {
6270
$this->attributeOptionProvider = $attributeOptionProvider;
6371
$this->layerFormatter = $layerFormatter;
72+
$this->YesNo = $YesNo;
6473
$this->bucketNameFilter = \array_merge($this->bucketNameFilter, $bucketNameFilter);
6574
}
6675

@@ -87,7 +96,11 @@ public function build(AggregationInterface $aggregation, ?int $storeId): array
8796
isset($attribute['position']) ? $attribute['position'] : null
8897
);
8998

90-
$options = $this->getSortedOptions($bucket, isset($attribute['options']) ? $attribute['options'] : []);
99+
$options = $this->getSortedOptions(
100+
$bucket,
101+
isset($attribute['options']) ? $attribute['options'] : [],
102+
($attribute['attribute_type']) ? $attribute['attribute_type']: ''
103+
);
91104
foreach ($options as $option) {
92105
$result[$bucketName]['options'][] = $this->layerFormatter->buildItem(
93106
$option['label'],
@@ -168,9 +181,11 @@ function (AggregationValueInterface $value) {
168181
*
169182
* @param BucketInterface $bucket
170183
* @param array $optionLabels
184+
* @param string $attributeType
171185
* @return array
186+
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
172187
*/
173-
private function getSortedOptions(BucketInterface $bucket, array $optionLabels): array
188+
private function getSortedOptions(BucketInterface $bucket, array $optionLabels, string $attributeType): array
174189
{
175190
/**
176191
* Option labels array has been sorted
@@ -179,7 +194,16 @@ private function getSortedOptions(BucketInterface $bucket, array $optionLabels):
179194
foreach ($bucket->getValues() as $value) {
180195
$metrics = $value->getMetrics();
181196
$optionValue = $metrics['value'];
182-
$optionLabel = $optionLabels[$optionValue] ?? $optionValue;
197+
if (isset($optionLabels[$optionValue])) {
198+
$optionLabel = $optionLabels[$optionValue];
199+
} else {
200+
if ($attributeType === 'boolean') {
201+
$yesNoOptions = $this->YesNo->toArray();
202+
$optionLabel = $yesNoOptions[$optionValue];
203+
} else {
204+
$optionLabel = $optionValue;
205+
}
206+
}
183207
$options[$optionValue] = $metrics + ['label' => $optionLabel];
184208
}
185209

@@ -188,7 +212,7 @@ private function getSortedOptions(BucketInterface $bucket, array $optionLabels):
188212
*/
189213
foreach ($options as $optionId => $option) {
190214
if (!is_array($options[$optionId])) {
191-
unset($options[$optionId]);
215+
unset($options[$optionId]);
192216
}
193217
}
194218

app/code/Magento/CatalogGraphQl/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"magento/module-catalog-search": "*",
1515
"magento/framework": "*",
1616
"magento/module-graph-ql": "*",
17+
"magento/module-config": "*",
1718
"magento/module-advanced-search": "*"
1819
},
1920
"suggest": {

app/code/Magento/CatalogGraphQl/etc/module.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<module name="Magento_Store"/>
1414
<module name="Magento_Eav"/>
1515
<module name="Magento_GraphQl"/>
16+
<module name="Magento_Config"/>
1617
<module name="Magento_StoreGraphQl"/>
1718
<module name="Magento_EavGraphQl"/>
1819
</sequence>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ function ($a) {
3737
$booleanAggregation = reset($booleanAggregation);
3838
$this->assertEquals('Boolean Attribute', $booleanAggregation['label']);
3939
$this->assertEquals('boolean_attribute', $booleanAggregation['attribute_code']);
40-
$this->assertContainsEquals(['label' => '1', 'value' => '1', 'count' => '3'], $booleanAggregation['options']);
40+
$this->assertContainsEquals(['label' => 'Yes', 'value' => '1', 'count' => '3'], $booleanAggregation['options']);
4141

4242
$this->assertEquals(2, $booleanAggregation['count']);
4343
$this->assertCount(2, $booleanAggregation['options']);
44-
$this->assertContainsEquals(['label' => '0', 'value' => '0', 'count' => '2'], $booleanAggregation['options']);
44+
$this->assertContainsEquals(['label' => 'No', 'value' => '0', 'count' => '2'], $booleanAggregation['options']);
4545
}
4646

4747
/**

0 commit comments

Comments
 (0)