Skip to content

Commit 7912ff6

Browse files
committed
Merge remote-tracking branch 'origin/MC-31497' into honey-235-bug-pr
2 parents c252c18 + 920bc4a commit 7912ff6

File tree

6 files changed

+257
-235
lines changed

6 files changed

+257
-235
lines changed

app/code/Magento/CatalogGraphQl/Model/Config/FilterAttributeReader.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
use Magento\Framework\Config\ReaderInterface;
1111
use Magento\Framework\GraphQl\Schema\Type\Entity\MapperInterface;
1212
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
13-
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection;
1413
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
1514

1615
/**
1716
* Adds custom/eav attributes to product filter type in the GraphQL config.
1817
*
1918
* Product Attribute should satisfy the following criteria:
20-
* - Attribute is searchable
21-
* - "Visible in Advanced Search" is set to "Yes"
22-
* - Attribute of type "Select" must have options
19+
* - (Attribute is searchable AND "Visible in Advanced Search" is set to "Yes")
20+
* - OR attribute is "Used in Layered Navigation"
21+
* - AND Attribute of type "Select" must have options
2322
*/
2423
class FilterAttributeReader implements ReaderInterface
2524
{
@@ -77,7 +76,7 @@ public function read($scope = null) : array
7776
$typeNames = $this->mapper->getMappedTypes(self::ENTITY_TYPE);
7877
$config = [];
7978

80-
foreach ($this->getAttributeCollection() as $attribute) {
79+
foreach ($this->getFilterAttributes() as $attribute) {
8180
$attributeCode = $attribute->getAttributeCode();
8281

8382
foreach ($typeNames as $typeName) {
@@ -120,15 +119,25 @@ private function getFilterType(Attribute $attribute): string
120119
}
121120

122121
/**
123-
* Create attribute collection
122+
* Get attributes to use in product filter input
124123
*
125-
* @return Collection|\Magento\Catalog\Model\ResourceModel\Eav\Attribute[]
124+
* @return array
126125
*/
127-
private function getAttributeCollection()
126+
private function getFilterAttributes(): array
128127
{
129-
return $this->collectionFactory->create()
128+
$filterableAttributes = $this->collectionFactory
129+
->create()
130+
->addHasOptionsFilter()
131+
->addIsFilterableFilter()
132+
->getItems();
133+
134+
$searchableAttributes = $this->collectionFactory
135+
->create()
130136
->addHasOptionsFilter()
131137
->addIsSearchableFilter()
132-
->addDisplayInAdvancedSearchFilter();
138+
->addDisplayInAdvancedSearchFilter()
139+
->getItems();
140+
141+
return $filterableAttributes + $searchableAttributes;
133142
}
134143
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/FilterArgument/ProductEntityAttributesForAst.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
*/
1818
class ProductEntityAttributesForAst implements FieldEntityAttributesInterface
1919
{
20+
private const PRODUCT_BASE_TYPE = 'SimpleProduct';
21+
22+
private const PRODUCT_FILTER_INPUT = 'ProductAttributeFilterInput';
23+
2024
/**
2125
* @var ConfigInterface
2226
*/
@@ -51,9 +55,9 @@ public function __construct(
5155
*/
5256
public function getEntityAttributes() : array
5357
{
54-
$productTypeSchema = $this->config->getConfigElement('SimpleProduct');
58+
$productTypeSchema = $this->config->getConfigElement(self::PRODUCT_BASE_TYPE);
5559
if (!$productTypeSchema instanceof Type) {
56-
throw new \LogicException(__("SimpleProduct type not defined in schema."));
60+
throw new \LogicException(__(self::PRODUCT_BASE_TYPE . " type not defined in schema."));
5761
}
5862

5963
$fields = [];
@@ -69,6 +73,9 @@ public function getEntityAttributes() : array
6973
}
7074
}
7175

76+
$productAttributeFilterFields = $this->getProductAttributeFilterFields();
77+
$fields = array_merge($fields, $productAttributeFilterFields);
78+
7279
foreach ($this->additionalAttributes as $attributeName) {
7380
$fields[$attributeName] = [
7481
'type' => 'String',
@@ -78,4 +85,24 @@ public function getEntityAttributes() : array
7885

7986
return $fields;
8087
}
88+
89+
/**
90+
* Get fields from ProductAttributeFilterInput
91+
*
92+
* @return array
93+
*/
94+
private function getProductAttributeFilterFields()
95+
{
96+
$filterFields = [];
97+
98+
$productAttributeFilterSchema = $this->config->getConfigElement(self::PRODUCT_FILTER_INPUT);
99+
$productAttributeFilterFields = $productAttributeFilterSchema->getFields();
100+
foreach ($productAttributeFilterFields as $filterField) {
101+
$filterFields[$filterField->getName()] = [
102+
'type' => 'String',
103+
'fieldName' => $filterField->getName(),
104+
];
105+
}
106+
return $filterFields;
107+
}
81108
}

0 commit comments

Comments
 (0)