Skip to content

Commit 54ef0f7

Browse files
committed
ACP2E-1172: categoryList Graphql request show 0 product count
1 parent e0d650c commit 54ef0f7

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Category/ProductsCount.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
namespace Magento\CatalogGraphQl\Model\Resolver\Category;
99

1010
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\CategoryRepository;
1112
use Magento\Catalog\Model\Product\Visibility;
1213
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CompositeCollectionProcessor;
1314
use Magento\Framework\Api\SearchCriteriaInterface;
14-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15-
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Framework\App\ObjectManager;
1616
use Magento\Framework\GraphQl\Config\Element\Field;
17+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1718
use Magento\Framework\GraphQl\Query\ResolverInterface;
18-
19+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1920

2021
/**
2122
* Retrieves products count for a category
@@ -37,19 +38,27 @@ class ProductsCount implements ResolverInterface
3738
*/
3839
private $searchCriteria;
3940

41+
/**
42+
* @var CategoryRepository
43+
*/
44+
private $categoryRepository;
45+
4046
/**
4147
* @param Visibility $catalogProductVisibility
4248
* @param SearchCriteriaInterface $searchCriteria
4349
* @param CompositeCollectionProcessor $collectionProcessor
50+
* @param CategoryRepository|null $categoryRepository
4451
*/
4552
public function __construct(
4653
Visibility $catalogProductVisibility,
4754
SearchCriteriaInterface $searchCriteria,
48-
CompositeCollectionProcessor $collectionProcessor
55+
CompositeCollectionProcessor $collectionProcessor,
56+
?CategoryRepository $categoryRepository = null
4957
) {
5058
$this->catalogProductVisibility = $catalogProductVisibility;
5159
$this->searchCriteria = $searchCriteria;
5260
$this->collectionProcessor = $collectionProcessor;
61+
$this->categoryRepository = $categoryRepository ?? ObjectManager::getInstance()->get(CategoryRepository::class);
5362
}
5463

5564
/**
@@ -62,6 +71,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6271
}
6372
/** @var Category $category */
6473
$category = $value['model'];
74+
75+
if (!$category->getIsAnchor() !== null) {
76+
$category->setIsAnchor($this->categoryRepository->get($value['id'])->getIsAnchor());
77+
}
6578
$productsCollection = $category->getProductCollection();
6679
$productsCollection->setVisibility($this->catalogProductVisibility->getVisibleInSiteIds());
6780
$productsCollection = $this->collectionProcessor->process(
@@ -70,8 +83,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7083
[],
7184
$context
7285
);
73-
$size = $productsCollection->getSize();
7486

75-
return $size;
87+
return $productsCollection->getSize();
7688
}
7789
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,4 +919,46 @@ public function testFilterStoreRootCategory() : void
919919
$this->assertArrayNotHasKey('errors', $result);
920920
$this->assertCount(1, $result['categoryList']);
921921
}
922+
923+
/**
924+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
925+
*/
926+
public function testQueryParentCategoriesProductCount()
927+
{
928+
$query = <<<QUERY
929+
{
930+
categoryList(filters: {ids: {eq: "3"}}){
931+
id
932+
name
933+
product_count
934+
level
935+
children{
936+
name
937+
product_count
938+
level
939+
children{
940+
name
941+
product_count
942+
level
943+
children{
944+
name
945+
product_count
946+
level
947+
children {
948+
name
949+
product_count
950+
level
951+
}
952+
}
953+
}
954+
}
955+
}
956+
}
957+
QUERY;
958+
$response = $this->graphQlQuery($query);
959+
$this->assertArrayNotHasKey('errors', $response);
960+
$this->assertArrayHasKey('categoryList', $response);
961+
$baseCategory = $response['categoryList'][0];
962+
$this->assertEquals(3, $baseCategory['product_count']);
963+
}
922964
}

0 commit comments

Comments
 (0)