Skip to content

Commit afae655

Browse files
committed
B2B-2469: Improve category children loading
- functional fixes
1 parent 69ef63a commit afae655

File tree

4 files changed

+9
-104
lines changed

4 files changed

+9
-104
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7272
$this->checkCategoryIsActive->execute($rootCategoryId);
7373
}
7474
$store = $context->getExtensionAttributes()->getStore();
75-
$categoriesTree = $this->categoryTree->getTreeCollection($info, $rootCategoryId);
75+
$categoriesTree = $this->categoryTree->getTreeCollection($info, $rootCategoryId, (int)$store->getId());
7676

7777
if ($categoriesTree->count() == 0) {
7878
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));

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

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -94,86 +94,19 @@ public function __construct(
9494
$this->collectionProcessor = $collectionProcessor;
9595
}
9696

97-
/**
98-
* Returns categories tree starting from parent $rootCategoryId
99-
*
100-
* @param ResolveInfo $resolveInfo
101-
* @param int $rootCategoryId
102-
* @param int $storeId
103-
* @return Iterator
104-
* @throws LocalizedException
105-
* @throws Exception
106-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
107-
*/
108-
public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId, int $storeId): Iterator
109-
{
110-
$collection = $this->getCollection($resolveInfo, $rootCategoryId);
111-
return $collection->getIterator();
112-
}
113-
11497
/**
11598
* Returns categories collection for tree starting from parent $rootCategoryId
11699
*
117100
* @param ResolveInfo $resolveInfo
118101
* @param int $rootCategoryId
102+
* @param int $storeId
119103
* @return Collection
120104
* @throws LocalizedException
121-
* @throws Exception
122105
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
123106
*/
124-
public function getTreeCollection(ResolveInfo $resolveInfo, int $rootCategoryId): Collection
107+
public function getTreeCollection(ResolveInfo $resolveInfo, int $rootCategoryId, int $storeId): Collection
125108
{
126-
return $this->getRawCollection($resolveInfo, [$rootCategoryId]);
127-
}
128-
129-
/**
130-
* Return prepared collection
131-
*
132-
* @param ResolveInfo $resolveInfo
133-
* @param int $rootCategoryId
134-
* @return Collection
135-
* @throws LocalizedException
136-
* @throws Exception
137-
*/
138-
private function getCollection(ResolveInfo $resolveInfo, int $rootCategoryId) : Collection
139-
{
140-
$categoryQuery = $resolveInfo->fieldNodes[0];
141-
$collection = $this->collectionFactory->create();
142-
$this->joinAttributesRecursively($collection, $categoryQuery, $resolveInfo);
143-
$depth = $this->depthCalculator->calculate($resolveInfo, $categoryQuery);
144-
$level = $this->levelCalculator->calculate($rootCategoryId);
145-
146-
// If root category is being filter, we've to remove first slash
147-
if ($rootCategoryId == Category::TREE_ROOT_ID) {
148-
$regExpPathFilter = sprintf('.*%s/[/0-9]*$', $rootCategoryId);
149-
} else {
150-
$regExpPathFilter = sprintf('.*/%s/[/0-9]*$', $rootCategoryId);
151-
}
152-
153-
//Add `is_anchor` attribute to selected field
154-
$collection->addAttributeToSelect('is_anchor');
155-
156-
//Search for desired part of category tree
157-
$collection->addPathFilter($regExpPathFilter);
158-
159-
$collection->addFieldToFilter('level', ['gt' => $level]);
160-
$collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]);
161-
$collection->addAttributeToFilter('is_active', 1, "left");
162-
$collection->setOrder('level');
163-
$collection->setOrder(
164-
'position',
165-
$collection::SORT_ORDER_DESC
166-
);
167-
$collection->getSelect()->orWhere(
168-
$collection->getSelect()
169-
->getConnection()
170-
->quoteIdentifier(
171-
'e.' . $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
172-
) . ' = ?',
173-
$rootCategoryId
174-
);
175-
176-
return $collection;
109+
return $this->getRawTreeCollection($resolveInfo, [$rootCategoryId]);
177110
}
178111

179112
/**
@@ -205,33 +138,6 @@ private function joinAttributesRecursively(
205138
}
206139
}
207140

208-
/**
209-
* Returns categories tree starting from parent $rootCategoryId with filtration
210-
*
211-
* @param ResolveInfo $resolveInfo
212-
* @param int $rootCategoryId
213-
* @param SearchCriteria $searchCriteria
214-
* @param StoreInterface $store
215-
* @param array $attributeNames
216-
* @param ContextInterface $context
217-
* @return Iterator
218-
* @throws LocalizedException
219-
* @throws Exception
220-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
221-
*/
222-
public function getFilteredTree(
223-
ResolveInfo $resolveInfo,
224-
int $rootCategoryId,
225-
SearchCriteria $searchCriteria,
226-
StoreInterface $store,
227-
array $attributeNames,
228-
ContextInterface $context
229-
): Iterator {
230-
$collection = $this->getCollection($resolveInfo, $rootCategoryId);
231-
$this->collectionProcessor->process($collection, $searchCriteria, $attributeNames, $context);
232-
return $collection->getIterator();
233-
}
234-
235141
/**
236142
* Returns categories tree starting from parent $rootCategoryId with filtration
237143
*
@@ -252,7 +158,7 @@ public function getFlatCategoriesByRootIds(
252158
array $attributeNames,
253159
ContextInterface $context
254160
): Collection {
255-
$collection = $this->getRawCollection($resolveInfo, $topLevelCategoryIds);
161+
$collection = $this->getRawTreeCollection($resolveInfo, $topLevelCategoryIds);
256162
$this->collectionProcessor->process($collection, $searchCriteria, $attributeNames, $context);
257163
return $collection;
258164
}
@@ -261,12 +167,11 @@ public function getFlatCategoriesByRootIds(
261167
* Return prepared collection
262168
*
263169
* @param ResolveInfo $resolveInfo
264-
* @param int $rootCategoryId
170+
* @param array $topLevelCategoryIds
265171
* @return Collection
266172
* @throws LocalizedException
267-
* @throws Exception
268173
*/
269-
private function getRawCollection(ResolveInfo $resolveInfo, array $topLevelCategoryIds) : Collection
174+
private function getRawTreeCollection(ResolveInfo $resolveInfo, array $topLevelCategoryIds) : Collection
270175
{
271176
$categoryQuery = $resolveInfo->fieldNodes[0];
272177
$collection = $this->collectionFactory->create();

app/code/Magento/CatalogUrlRewriteGraphQl/Model/DataProvider/UrlRewrite/CatalogTreeDataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function getData(
6363
int $storeId = null
6464
): array {
6565
$categoryId = (int)$id;
66-
$categoriesTree = $this->categoryTree->getTreeCollection($info, $categoryId);
66+
$categoriesTree = $this->categoryTree->getTreeCollection($info, $categoryId, $storeId);
6767
if ($categoriesTree->count() == 0) {
6868
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
6969
}

dev/tests/integration/testsuite/Magento/Catalog/_files/product_in_multiple_categories.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)->setParentId(
1717
2
1818
)->setPath(
19-
'1/2/3'
19+
'1/2/333'
2020
)->setLevel(
2121
2
2222
)->setAvailableSortBy(

0 commit comments

Comments
 (0)