1

I want to get all categories of the specific category for that I tried

public function categoryHide()
    {
        $data = $this->arrayData();
        $save = array();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
        $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');// Instance of Category Model
        foreach($data as $test)
        {
            $category = $categoryFactory->create()->load($test);
            // $parentCategories = $category->getParentCategories();
            $childrenCategories = $category->getChildrenCategories();
            foreach ($childrenCategories as $cat){
                $save[] = $cat->getId();
              }
        }
        return array_merge($data,$save);   
    }

}

This code is working perfectly for the Gear but if we tried to get the Ids of the Women or men category it will return only 2 children enter image description here

it will only return the ids of tops and bottoms not there further children eg for women its category is 20 and tops category is 21 and bottoms category is 22 its only return there values not further children of tops and bottom. now I want to get all the child categories ids. Thanks in advance

1

3 Answers 3

-1

you will have to solve this with recursion.

$category->getChildrenCategories() will only return direct children

public function categoryHide()
    {
        $data = $this->arrayData();
        $save = array();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
        $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');// Instance of Category Model
        foreach($data as $test)
        {
            $category = $categoryFactory->create()->load($test);
            addToSave($category, $save);
        }
        return array_merge($data,$save);   
    }

}

public function addToSave($category, &$save)
{
    $childrenCategories = $category->getChildrenCategories();
    foreach ($childrenCategories as $cat){
        $save[] = $cat->getId();
        addToSave($cat, $save);
    }
} 
2
  • can u please help me how to solve this?
    – Asad Khan
    Feb 19, 2019 at 12:29
  • @AsadKhan see updated answer for code. I did not verify if this works Feb 19, 2019 at 12:35
2
$category = $categoryFactory->create()->load($id);
$category->getAllChildren(false) - will return comma separated list of ids
1

You can try the following code :

<?php 
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
 $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');// Instance of Category Model
 $categoryId = 15; // YOUR CATEGORY ID
 $category = $categoryFactory->create()->load($categoryId);
 // Children  Categories
 $childrenCategories = $category->getChildrenCategories();

?>

Get all level subcategories :

<?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
2
  • it only return direct children not all the child... I want to get all the children
    – Asad Khan
    Feb 19, 2019 at 12:32
  • @AsadKhan, I edited my answer please check it now Feb 19, 2019 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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