public function getCategoriesTree()
    {
        $allCategories = $this->getCategories();
        $topCategories = [];

        $currentCategory = null;

        // Building tree from categories collection
        foreach ($allCategories as $idCat => $category) {

            // If level, we put category in an array with only top level categories.
            $topLevel = 2; // 1 is root category
            if ($category->getLevel() == $topLevel && !isset($topCategories[$idCat])) {
                $topCategories[$idCat] = $category;
            } else {
                // Foreach categories with sub category we add a child to the parent
                if (!isset($allCategories[$category->getParentId()])) {
                    // Ignore if no parent
                    continue;
                }
                $parent           = $allCategories[$category->getParentId()];
                $children         = $parent->getData('_children') ?: [];
                $children[$idCat] = $category;
                $category->setData('_parent', $parent);
                $parent->setData('_children', $children);
            }

            if ((int) $idCat === $this->getCurrentCategoryId()) {
                $currentCategory = $category;
            }
        }

        // Set current path
        if (null !== $currentCategory) {
            $setCategoryAsCurrent = function (Mage_Catalog_Model_Category $category, $isParent = false) use (&$setCategoryAsCurrent) {
                if ($isParent) {
                    $category->setContainsCurrentCategory(true);
                } else {
                    $category->setIsCurrentCategory(true);
                }
                if ($parentCategory = $category->getData('_parent')) {
                    $setCategoryAsCurrent($parentCategory, true);
                }
            };
            $setCategoryAsCurrent($currentCategory);
        }

        return $topCategories;
    }
public function getCategoriesTree()
    {
        $allCategories = $this->getCategories();
        $topCategories = [];

        $currentCategory = null;

        // Building tree from categories collection
        foreach ($allCategories as $idCat => $category) {

            // If level, we put category in an array with only top level categories.
            $topLevel = 2; // 1 is root category
            if ($category->getLevel() == $topLevel && !isset($topCategories[$idCat])) {
                $topCategories[$idCat] = $category;
            } else {
                // Foreach categories with sub category we add a child to the parent
                if (!isset($allCategories[$category->getParentId()])) {
                    // Ignore if no parent
                    continue;
                }
                $parent           = $allCategories[$category->getParentId()];
                $children         = $parent->getData('_children') ?: [];
                $children[$idCat] = $category;
                $category->setData('_parent', $parent);
                $parent->setData('_children', $children);
            }

            if ((int) $idCat === $this->getCurrentCategoryId()) {
                $currentCategory = $category;
            }
        }

        // Set current path
        if (null !== $currentCategory) {
            $setCategoryAsCurrent = function (Mage_Catalog_Model_Category $category, $isParent = false) use (&$setCategoryAsCurrent) {
                if ($isParent) {
                    $category->setContainsCurrentCategory(true);
                } else {
                    $category->setIsCurrentCategory(true);
                }
                if ($parentCategory = $category->getData('_parent')) {
                    $setCategoryAsCurrent($parentCategory, true);
                }
            };
            $setCategoryAsCurrent($currentCategory);
        }

        return $topCategories;
    }
public function getCategoriesTree ( ) { $ allCategories = $ this -> getCategories ( ) ; $ topCategories = [ ] ; $ currentCategory = null ; // Building tree from categories collection foreach ( $ allCategories as $ idCat => $ category ) { // If level, we put category in an array with only top level categories. $ topLevel = 2 ; // 1 is root category if ( $ category -> getLevel ( ) == $ topLevel && ! isset ( $ topCategories [ $ idCat ] ) ) { $ topCategories [ $ idCat ] = $ category ; } else { // Foreach categories with sub category we add a child to the parent if ( ! isset ( $ allCategories [ $ category -> getParentId ( ) ] ) ) { // Ignore if no parent continue ; } $ parent = $ allCategories [ $ category -> getParentId ( ) ] ; $ children = $ parent -> getData ( '_children' ) ? : [ ] ; $ children [ $ idCat ] = $ category ; $ category -> setData ( '_parent' , $ parent ) ; $ parent -> setData ( '_children' , $ children ) ; } if ( ( int ) $ idCat === $ this -> getCurrentCategoryId ( ) ) { $ currentCategory = $ category ; } } // Set current path if ( null !== $ currentCategory ) { $ setCategoryAsCurrent = function ( Mage_Catalog_Model_Category $ category , $ isParent = false ) use ( & $ setCategoryAsCurrent ) { if ( $ isParent ) { $ category -> setContainsCurrentCategory ( true ) ; } else { $ category -> setIsCurrentCategory ( true ) ; } if ( $ parentCategory = $ category -> getData ( '_parent' ) ) { $ setCategoryAsCurrent ( $ parentCategory , true ) ; } } ; $ setCategoryAsCurrent ( $ currentCategory ) ; } return $ topCategories ; }
Retrieve the categories tree
@return array Array of the main categories with their children
Retrieve the categories tree