0

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;        

    }
  • I want get all level of subcategories. I can't predict the level of subcategories. The link gives only 2 levels of category – Jancy Abraham Oct 9 '17 at 6:32
1

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
  • Display the subcategories as structured – Jancy Abraham Oct 9 '17 at 6:33
  • to extend this, how can I get sorted children categories - sorted by category name? – Kamlesh Yaduwanshi Aug 9 '19 at 12:02
  • @KamleshYaduwanshi You can sort the array $subcategories = explode(',', $categories->getChildren()); as per your requirement. – Dinesh Yadav Aug 12 '19 at 10:42

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