문제

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).

도움이 되었습니까?

해결책

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

다른 팁

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;
}

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": []
        }
    ]
}

Another way to get Top Categories is

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

You can add depth according to you requirments.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top