Skip to content

Commit cbca71e

Browse files
author
Oleksandr Iegorov
committed
MC-29026: GraphQL returns filters with some data if you making request to not existing or empty category
1 parent e62c675 commit cbca71e

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Layer/DataProvider/Filters.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ public function __construct(
3434
* Get layered navigation filters data
3535
*
3636
* @param string $layerType
37+
* @param array|null $attributesToFilter
3738
* @return array
39+
* @throws \Magento\Framework\Exception\LocalizedException
3840
*/
39-
public function getData(string $layerType) : array
41+
public function getData(string $layerType, array $attributesToFilter = null) : array
4042
{
4143
$filtersData = [];
4244
/** @var AbstractFilter $filter */
4345
foreach ($this->filtersProvider->getFilters($layerType) as $filter) {
44-
if ($filter->getItemsCount()) {
46+
if ($this->isNeedToAddFilter($filter, $attributesToFilter)) {
4547
$filterGroup = [
4648
'name' => (string)$filter->getName(),
4749
'filter_items_count' => $filter->getItemsCount(),
@@ -60,4 +62,19 @@ public function getData(string $layerType) : array
6062
}
6163
return $filtersData;
6264
}
65+
66+
private function isNeedToAddFilter(AbstractFilter $filter, array $attributesToFilter): bool
67+
{
68+
if ($attributesToFilter === null) {
69+
$result = (bool)$filter->getItemsCount();
70+
} else {
71+
try {
72+
$filterAttribute = $filter->getAttributeModel();
73+
$result = in_array($filterAttribute->getAttributeCode(), $attributesToFilter);
74+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
75+
$result = false;
76+
}
77+
}
78+
return $result;
79+
}
6380
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ public function resolve(
4444
return null;
4545
}
4646

47-
return $this->filtersDataProvider->getData($value['layer_type']);
47+
$attributes = $this->prepareAttributesResults($value);
48+
return $this->filtersDataProvider->getData($value['layer_type'], $attributes);
49+
}
50+
51+
/**
52+
* Get attributes available to filtering from the search result
53+
*
54+
* @param array $value
55+
* @return array|null
56+
*/
57+
private function prepareAttributesResults(array $value): ?array
58+
{
59+
$attributes = [];
60+
if (!empty($value['search_result'])) {
61+
$buckets = $value['search_result']->getSearchAggregation()->getBuckets();
62+
foreach ($buckets as $bucket) {
63+
if (!empty($bucket->getValues())) {
64+
$attributes[] = str_replace('_bucket', '', $bucket->getName());
65+
}
66+
}
67+
} else {
68+
$attributes = null;
69+
}
70+
return $attributes;
4871
}
4972
}

app/code/Magento/SwatchesGraphQl/Plugin/Filters/DataProviderPlugin.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,17 @@ public function __construct(
5353
* @param Filters $subject
5454
* @param \Closure $proceed
5555
* @param string $layerType
56+
* @param array $attributesToFilter
5657
* @return array
5758
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5859
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5960
*/
60-
public function aroundGetData(Filters $subject, \Closure $proceed, string $layerType) : array
61-
{
61+
public function aroundGetData(
62+
Filters $subject,
63+
\Closure $proceed,
64+
string $layerType,
65+
array $attributesToFilter = null
66+
) : array {
6267
$swatchFilters = [];
6368
/** @var AbstractFilter $filter */
6469
foreach ($this->filtersProvider->getFilters($layerType) as $filter) {
@@ -69,7 +74,7 @@ public function aroundGetData(Filters $subject, \Closure $proceed, string $layer
6974
}
7075
}
7176

72-
$filtersData = $proceed($layerType);
77+
$filtersData = $proceed($layerType, $attributesToFilter);
7378

7479
foreach ($filtersData as $groupKey => $filterGroup) {
7580
/** @var AbstractFilter $swatchFilter */

0 commit comments

Comments
 (0)