Hello there.
I am using a fresh install Magento 2.3.2 CE with sample data, and I work extensively with Magento 2 REST API to integrate it with my python3-based app.
The problem is, when I make a simple GET call to http://localhost/rest/all/V1/categories
response code is 200, and it returns me this:
{'id': 1, 'parent_id': 0, 'name': None, 'is_active': None, 'position': 0, 'level': 0, 'product_count': None, 'children_data': [{'id': 2, 'parent_id': 1, 'name': None, 'is_active': None, 'position': 1, 'level': 1, 'product_count': None, 'children_data': [{'id': 38, 'parent_id': 2, 'name': None, 'is_active': None, 'position': 1, 'level': 2, 'product_count': None, 'children_data': []},
....
and list goes on, all category names are "None".
I tried indexer:reindex and cache:clean. None of those helped.
It's important part of my app to get categories names, so I can't skip it.
PS. I tried to do the same with 2.3.0 fresh install, and it worked just fine.
Is it a 2.3.2 bug? How do I solve that?
Thanks.
Hello @overjolted
Open vendor/magento/module-catalog/Model/Category/Tree.php
Replace the function getTree with the below code:
public function getTree($node, $depth = null, $currentLevel = 0) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); $categoryId = $node->getId(); $nodeS = $categoryFactory->create()->load($categoryId); /** @var \Magento\Catalog\Api\Data\CategoryTreeInterface[] $children */ $children = $this->getChildren($node, $depth, $currentLevel); /** @var \Magento\Catalog\Api\Data\CategoryTreeInterface $tree */ $tree = $this->treeFactory->create(); $tree->setId($nodeS->getId()) ->setParentId($nodeS->getParentId()) ->setName($nodeS->getName()) ->setPosition($nodeS->getPosition()) ->setLevel($nodeS->getLevel()) ->setIsActive($nodeS->getIsActive()) ->setProductCount($nodeS->getProductCount()) ->setChildrenData($children); return $tree; }
The below screenshot shows how we implemented this code to get the solution:
Hope it helps.
Hi @Meetanshi
Your solution is work But It will impact to performance web site when we load subcategory.
@overjolted Can you try my solution It will work and not impact performance website:
override
Magento\Catalog\Model\Category\Tree
protected function getNode(\Magento\Catalog\Model\Category $category)
{
$nodeId = $category->getId();
$categoryTree = $this->treeResourceFactory->create();
$node = $categoryTree->loadNode($nodeId);
$node->loadChildren();
$this->prepareCollection();
$categoryTree->addCollectionData($this->categoryCollection);
return $node;
}
Hope it helps.
Bun!