1

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()) {
            continue;
         }
        $parentCatIds['parent'][] =  $category->getId().$category->getName();
        if($childrenCategories = $this->getChildCategories($category)):
            foreach($childrenCategories as $childrenCategory):
                 if (!$childrenCategory->getIsActive()) {
                    continue;
                 }
                 $childrenCategoryIds['clild'][] = $childrenCategory->getId().$childrenCategory->getName();
            endforeach;
        endif;
    endforeach;
    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();
            /*if($this->getChildCategories($subcategories)):
                getChildCategories($subcategories);
            endif;*/
        }
        return $subcategories;
}
  • Hi @shybhjay das are you looking for a solution like this? – NIPPU 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.