Skip to content

Commit 72f508e

Browse files
committed
Merge remote-tracking branch 'tango/MC-29026' into Chaika-PR-2019-11-25
2 parents 2fee29e + 004d61a commit 72f508e

File tree

4 files changed

+110
-7
lines changed

4 files changed

+110
-7
lines changed

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
1111
use Magento\CatalogGraphQl\Model\Resolver\Layer\FiltersProvider;
12+
use Magento\Catalog\Model\Layer\Filter\Item;
1213

1314
/**
1415
* Layered navigation filters data provider.
@@ -20,6 +21,11 @@ class Filters
2021
*/
2122
private $filtersProvider;
2223

24+
/**
25+
* @var array
26+
*/
27+
private $mappings;
28+
2329
/**
2430
* Filters constructor.
2531
* @param FiltersProvider $filtersProvider
@@ -28,26 +34,31 @@ public function __construct(
2834
FiltersProvider $filtersProvider
2935
) {
3036
$this->filtersProvider = $filtersProvider;
37+
$this->mappings = [
38+
'Category' => 'category'
39+
];
3140
}
3241

3342
/**
3443
* Get layered navigation filters data
3544
*
3645
* @param string $layerType
46+
* @param array|null $attributesToFilter
3747
* @return array
48+
* @throws \Magento\Framework\Exception\LocalizedException
3849
*/
39-
public function getData(string $layerType) : array
50+
public function getData(string $layerType, array $attributesToFilter = null) : array
4051
{
4152
$filtersData = [];
4253
/** @var AbstractFilter $filter */
4354
foreach ($this->filtersProvider->getFilters($layerType) as $filter) {
44-
if ($filter->getItemsCount()) {
55+
if ($this->isNeedToAddFilter($filter, $attributesToFilter)) {
4556
$filterGroup = [
4657
'name' => (string)$filter->getName(),
4758
'filter_items_count' => $filter->getItemsCount(),
4859
'request_var' => $filter->getRequestVar(),
4960
];
50-
/** @var \Magento\Catalog\Model\Layer\Filter\Item $filterItem */
61+
/** @var Item $filterItem */
5162
foreach ($filter->getItems() as $filterItem) {
5263
$filterGroup['filter_items'][] = [
5364
'label' => (string)$filterItem->getLabel(),
@@ -60,4 +71,32 @@ public function getData(string $layerType) : array
6071
}
6172
return $filtersData;
6273
}
74+
75+
/**
76+
* Check for adding filter to the list
77+
*
78+
* @param AbstractFilter $filter
79+
* @param array $attributesToFilter
80+
* @return bool
81+
* @throws \Magento\Framework\Exception\LocalizedException
82+
*/
83+
private function isNeedToAddFilter(AbstractFilter $filter, array $attributesToFilter): bool
84+
{
85+
if ($attributesToFilter === null) {
86+
$result = (bool)$filter->getItemsCount();
87+
} else {
88+
if ($filter->hasAttributeModel()) {
89+
$filterAttribute = $filter->getAttributeModel();
90+
$result = in_array($filterAttribute->getAttributeCode(), $attributesToFilter);
91+
} else {
92+
$name = (string)$filter->getName();
93+
if (array_key_exists($name, $this->mappings)) {
94+
$result = in_array($this->mappings[$name], $attributesToFilter);
95+
} else {
96+
$result = true;
97+
}
98+
}
99+
}
100+
return $result;
101+
}
63102
}

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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class DataProviderPlugin
3636
*
3737
* @param FiltersProvider $filtersProvider
3838
* @param \Magento\Swatches\Helper\Data $swatchHelper
39+
* @param \Magento\Swatches\Block\LayeredNavigation\RenderLayered $renderLayered
3940
*/
4041
public function __construct(
4142
FiltersProvider $filtersProvider,
@@ -53,12 +54,19 @@ public function __construct(
5354
* @param Filters $subject
5455
* @param \Closure $proceed
5556
* @param string $layerType
57+
* @param array|null $attributesToFilter
5658
* @return array
59+
* @throws \Magento\Framework\Exception\LocalizedException
5760
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5861
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
62+
* phpcs:disable Generic.Metrics.NestingLevel
5963
*/
60-
public function aroundGetData(Filters $subject, \Closure $proceed, string $layerType) : array
61-
{
64+
public function aroundGetData(
65+
Filters $subject,
66+
\Closure $proceed,
67+
string $layerType,
68+
$attributesToFilter = null
69+
) : array {
6270
$swatchFilters = [];
6371
/** @var AbstractFilter $filter */
6472
foreach ($this->filtersProvider->getFilters($layerType) as $filter) {
@@ -69,7 +77,7 @@ public function aroundGetData(Filters $subject, \Closure $proceed, string $layer
6977
}
7078
}
7179

72-
$filtersData = $proceed($layerType);
80+
$filtersData = $proceed($layerType, $attributesToFilter);
7381

7482
foreach ($filtersData as $groupKey => $filterGroup) {
7583
/** @var AbstractFilter $swatchFilter */
@@ -92,4 +100,5 @@ public function aroundGetData(Filters $subject, \Closure $proceed, string $layer
92100

93101
return $filtersData;
94102
}
103+
//phpcs:enable
95104
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@
3131
*/
3232
class ProductSearchTest extends GraphQlAbstract
3333
{
34+
/**
35+
* Verify that filters for non-existing category are empty
36+
*
37+
* @throws \Exception
38+
*/
39+
public function testFilterForNonExistingCategory()
40+
{
41+
$query = <<<QUERY
42+
{
43+
products(filter: {category_id: {eq: "99999999"}}) {
44+
filters {
45+
name
46+
}
47+
}
48+
}
49+
QUERY;
50+
51+
$response = $this->graphQlQuery($query);
52+
53+
$this->assertArrayHasKey(
54+
'filters',
55+
$response['products'],
56+
'Filters are missing in product query result.'
57+
);
58+
59+
$this->assertEmpty(
60+
$response['products']['filters'],
61+
'Returned filters data set does not empty'
62+
);
63+
}
64+
3465
/**
3566
* Verify that layered navigation filters and aggregations are correct for product query
3667
*
@@ -41,6 +72,7 @@ class ProductSearchTest extends GraphQlAbstract
4172
*/
4273
public function testFilterLn()
4374
{
75+
$this->reIndexAndCleanCache();
4476
$query = <<<QUERY
4577
{
4678
products (

0 commit comments

Comments
 (0)