Skip to content

Commit e1aa929

Browse files
committed
Query category for disabled catalog with products returns the products count
1 parent 6c6047d commit e1aa929

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterato
8585
{
8686
$categoryQuery = $resolveInfo->fieldNodes[0];
8787
$collection = $this->collectionFactory->create();
88+
if ($this->isRootCategoryActive($collection, $rootCategoryId) == false) {
89+
throw new \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
90+
}
8891
$this->joinAttributesRecursively($collection, $categoryQuery);
8992
$depth = $this->depthCalculator->calculate($categoryQuery);
9093
$level = $this->levelCalculator->calculate($rootCategoryId);
@@ -144,4 +147,29 @@ private function joinAttributesRecursively(Collection $collection, FieldNode $fi
144147
$this->joinAttributesRecursively($collection, $node);
145148
}
146149
}
150+
151+
/**
152+
* @param Collection $collection
153+
* @param int $rootCategoryId
154+
*
155+
* @return bool
156+
* @throws \Exception
157+
*/
158+
private function isRootCategoryActive(Collection $collection, int $rootCategoryId) : bool
159+
{
160+
if ($rootCategoryId == Category::TREE_ROOT_ID) {
161+
return true;
162+
}
163+
$collection->addAttributeToFilter(Category::KEY_IS_ACTIVE, ['eq' => 1])
164+
->getSelect()
165+
->where(
166+
$collection->getSelect()
167+
->getConnection()
168+
->quoteIdentifier(
169+
'e.' . $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
170+
) . ' = ?',
171+
$rootCategoryId
172+
);
173+
return (bool) $collection->count();
174+
}
147175
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ExtractDataFromCategoryTree.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
use Magento\CatalogGraphQl\Model\Category\Hydrator;
1111
use Magento\Catalog\Api\Data\CategoryInterface;
12-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13-
use Magento\Catalog\Model\Category;
1412

1513
/**
1614
* Extract data from category tree
@@ -53,9 +51,6 @@ public function execute(\Iterator $iterator): array
5351
while ($iterator->valid()) {
5452
/** @var CategoryInterface $category */
5553
$category = $iterator->current();
56-
if ($category->getIsActive() == false && $category->getId() != Category::TREE_ROOT_ID) {
57-
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
58-
}
5954
$iterator->next();
6055
$pathElements = explode("/", $category->getPath());
6156
if (empty($tree)) {

0 commit comments

Comments
 (0)