Question

I want to get all the subcategories of a category(including subcategories of subcategories). I want to display it as per the structure. The below code display as

Array
(
    [121] => Array
        (
            [0] => Array
                (
                    [122] => Antibiotics
                    [127] => Pet Apparel                    
                )
        )

    [122] => Array
        (
            [1] => Array
                (
                    [123] => Fish/Bird
                    [124] => Injectables
                    [125] => Pill Helpers
                    [126] => Powders
                )
        )

    [127] => Array
        (
            [1] => Array
                (
                    [128] => Dog Clothing
                )
        )

    [128] => Array
        (
            [2] => Array
                (
                    [129] => Dog Jackets
                    [130] => Dog Costumes
                    [131] => Dog Apparel
                    [132] => Dog Hoddie
                )
        )

)

Here is my code

public function getParentCategory()
    {
        $parent_category_id = 0;
        if($this->getCurrentCategory()){
            if($this->getCurrentCategory()->getParentCategories()){
                foreach ($this->getCurrentCategory()->getParentCategories() as $parent) {
                    if ($parent->getLevel() == 2) {
                        $parent_category_id = $parent->getId();
                    }
                }
            }
        }
       return $this->getSubCategories(121,0);

        }
    public function getSubCategories($id,$level)    {


        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
        $categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');
        $categoryObj = $categoryRepository->get($id);
        $subcategories = $categoryObj->getChildrenCategories();
        foreach($subcategories as $subcategorie) {
            $this->categoryArray[$id] [$level][$subcategorie->getId()] = $subcategorie->getName();
            if($subcategorie->hasChildren()){
                $this->getSubCategories($subcategorie->getId(),$level+1);
            }
        }
        return $this->categoryArray;        

    }
Was it helpful?

Solution

You can get subcategories structure up to infinite level as below.

<?php

function categoryLoop($id){
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $categories = $objectManager->create('Magento\Catalog\Model\Category')->load($id);

    if($categories->hasChildren()){
    echo "<ul>";
        $subcategories = explode(',', $categories->getChildren());
        foreach ($subcategories as $category) {
            $subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
            echo "<li>";
            echo $subcategory->getName();
            echo "</li>";
            //echo "<br>";
            if($subcategory->hasChildren()){ categoryLoop($category); }
        }
    echo "</ul>";
    }
}

categoryLoop(2);  // enter your category id here
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top