i want to get all category id of a specific store in magneto2 including its all root category and all level nested child category id of all root category i am trying with following cod but using this i am getting only first level child category id only but child of child and again this child's child category i mean all level nested child i did't getting place help me thank you.

public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
    $parentCatList = $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
    $parentCatIds = array();
    $childrenCategoryIds = array();
    foreach($parentCatList as $category):
         if (!$category->getIsActive()) {
        $parentCatIds['parent'][] =  $category->getId().$category->getName();
        if($childrenCategories = $this->getChildCategories($category)):
            foreach($childrenCategories as $childrenCategory):
                 if (!$childrenCategory->getIsActive()) {
                 $childrenCategoryIds['clild'][] = $childrenCategory->getId().$childrenCategory->getName();
    return array_merge($parentCatIds,$childrenCategoryIds);
 * Retrieve child store categories
public function getChildCategories($category)
       if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) {
            $subcategories = (array)$category->getChildrenNodes();
        } else {
            $subcategories = $category->getChildren();
        return $subcategories;
  • Hi @shybhjay das are you looking for a solution like this? – Nagaraju K Nov 19 '18 at 6:46
  • hello @NagarajuKasa i want all nested category until end – Shubhajay Das Nov 19 '18 at 6:48
  • On which structure you want the category ids? I means can you add a sample array? – Sukumar Gorai Nov 19 '18 at 7:23
  • it will be better if get the array in an one dimension structure array all the category ids – Shubhajay Das Nov 19 '18 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.