2

I am using "rest/V1/categories/" (Magento 2 Rest API). In documentation(devdocs.magento.com/swagger/#) it says "Retrieve list of categories" but it only returns default category(and its all children) every time. How can i get the list of all categories(those which are parent including default) enter image description here

This is how i am doing it,

public function getCatagory($data){
        $response = array();
        if(isset($data['categoryId'])){
            $url = $this->baseUrl."rest/V1/categories/".$data['categoryId'];
            $response = $this->curlGet($url,$this->token);
        }else{
            $url = $this->baseUrl."rest/V1/categories/";
            $response = $this->curlGet($url,$this->token);
        }
        return $response;
    }

I am executing else part here "rest/V1/categories/" and i am getting,

{
  "id": 2,
  "parent_id": 1,
  "name": "Default Category",
  "is_active": true,
  "position": 1,
  "level": 1,
  "product_count": 0,
  "children_data": [

  ]
}

It only giving me default why are the other categories missing as shown in the attached image of admin panel. Please help!! Thanks i advance(I don't want to put all my categories under default as children, i know its a way out, but i have already made each of them a parent as they are shown in the images attached now i just want to fetch them).

0

4 Answers 4

11

Well you can easily get all your categories by passing "all" keyword in your categories get API URL

It will give you all the categories including all your root categories.

Try below Steps:

Note : Make sure you pass all keyword in Url and it will works

3
  • Well that works perfectly fine!! Can you please share with me the reference to the Magento documentation regarding the use of this "all" keyword? I couldn't find anything here devdocs.magento.com/swagger
    – Abdul Moiz
    Jun 29, 2017 at 13:57
  • 1
    @AbdulMoiz Happy to help :) ,actually there is no documentation for that , i have gone through rest api controller and its interface and find this kind of url . you can refer core module for that Jun 30, 2017 at 4:36
  • @ManthanDave thanks for the help. However, I need to get the category structure only for the website in which user is in? Can I do that?
    – Anto S
    Jun 24, 2019 at 13:02
2

Another way to get Top Categories is

/rest/default/V1/categories?rootCategoryId=2&depth=1

You can add depth according to you requirments.

0

This will help you get the category name. It works for me Data

public function getTree($node, $depth = null, $currentLevel = 0)
{
    /** @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($node->getId())
    //     ->setParentId($node->getParentId())
    //     ->setName($node->getName())
    //     ->setPosition($node->getPosition())
    //     ->setLevel($node->getLevel())
    //     ->setIsActive($node->getIsActive())
    //     ->setProductCount($node->getProductCount())
    //     ->setChildrenData($children);
    // return $tree;


   $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;
}
1
  • This question is about the REST API.
    – bfontaine
    Sep 12, 2023 at 14:32
0

For Magento 2.3.3 version its not giving the name in the response.

{
    "id": 1,
    "parent_id": 0,
    "name": null,
    "is_active": null,
    "position": 0,
    "level": 0,
    "product_count": null,
    "children_data": [
        {
            "id": 2,
            "parent_id": 1,
            "name": null,
            "is_active": null,
            "position": 1,
            "level": 1,
            "product_count": null,
            "children_data": []
        }
    ]
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.