Skip to content

Commit a505d6d

Browse files
committed
MC-15986: Category Filtering
- add changes in category filter
1 parent f644625 commit a505d6d

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

app/code/Magento/CatalogGraphQl/Model/Category/CategoryFilter.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace Magento\CatalogGraphQl\Model\Category;
99

10+
use Magento\Catalog\Api\Data\CategoryInterface;
1011
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
12+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
1113

1214
/**
1315
* Category filter allows to filter collection using 'id, url_key, name' from search criteria.
@@ -32,24 +34,47 @@ public function __construct(
3234
* Filter for filtering the requested categories id's based on url_key, ids, name in the result.
3335
*
3436
* @param array $args
35-
* @param \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
36-
* @return bool
37+
* @param Collection $categoryCollection
3738
*/
3839
public function applyFilters(
3940
array $args,
40-
\Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
41-
): bool {
41+
Collection $categoryCollection
42+
): void {
43+
$categoryCollection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, ['eq' => 1]);
4244
foreach ($args['filters'] as $field => $cond) {
4345
foreach ($cond as $condType => $value) {
4446
if ($field === 'ids') {
4547
$categoryCollection->addIdFilter($value);
46-
} elseif ($condType === 'match') {
47-
$categoryCollection->addAttributeToFilter($field, ['like' => "%{$value}%"]);
4848
} else {
49-
$categoryCollection->addAttributeToFilter($field, [$condType => $value]);
49+
$this->addAttributeFilter($categoryCollection, $field, $condType, $value);
5050
}
5151
}
5252
}
53-
return true;
53+
}
54+
55+
/**
56+
* @param Collection $categoryCollection
57+
* @param string $field
58+
* @param string $condType
59+
* @param string|array $value
60+
*/
61+
private function addAttributeFilter($categoryCollection, $field, $condType, $value)
62+
{
63+
if ($condType === 'match') {
64+
$this->addMatchFilter($categoryCollection, $field, $value);
65+
return;
66+
}
67+
$categoryCollection->addAttributeToFilter($field, [$condType => $value]);
68+
}
69+
70+
/**
71+
*
72+
* @param Collection $categoryCollection
73+
* @param string $field
74+
* @param string $value
75+
*/
76+
private function addMatchFilter($categoryCollection, $field, $value)
77+
{
78+
$categoryCollection->addAttributeToFilter($field, ['like' => "%{$value}%"]);
5479
}
5580
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10-
use Magento\Catalog\Model\Category;
1110
use Magento\CatalogGraphQl\Model\Resolver\Category\CheckCategoryIsActive;
1211
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
1312
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -77,10 +76,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7776
if (isset($value[$field->getName()])) {
7877
return $value[$field->getName()];
7978
}
80-
8179
$categoryCollection = $this->collectionFactory->create();
82-
$categoryCollection->addAttributeToFilter('is_active', 1);
83-
$categoryCollection->addAttributeToSelect(['name','url_key', 'ids']);
8480

8581
if (!isset($args['filters'])) {
8682
$rootCategoryIds = [(int)$context->getExtensionAttributes()->getStore()->getRootCategoryId()];
@@ -91,14 +87,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
9187
$rootCategoryIds[] = (int)$category->getId();
9288
}
9389
}
94-
9590
$result = [];
9691
foreach ($rootCategoryIds as $rootCategoryId) {
97-
if ($rootCategoryId !== Category::TREE_ROOT_ID) {
98-
$this->checkCategoryIsActive->execute($rootCategoryId);
99-
}
10092
$categoryTree = $this->categoryTree->getTree($info, $rootCategoryId);
101-
if (empty($categoryTree) || ($categoryTree->count() == 0)) {
93+
if (empty($categoryTree)) {
10294
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
10395
}
10496
$result[] = current($this->extractDataFromCategoryTree->execute($categoryTree));

0 commit comments

Comments
 (0)