Magento recursive category navigation tree


I needed a category navigation in Magento that behaved in the following manner:
1. All categories directly under the store’s root category where displayed as a link
2. The navigation would drill down to the current category being viewed
3. The siblings of the current category would also be visible as a link

The other solutions out there were either not recursive, or they simply displayed a category tree that printed every category, so I ended up writing my own below. Place this code in /app/design/frontend/yourcompany/yourtheme/template/catalog/navigation/left.phtml .

<div class="block block-list block-categories">
    <div id="leftnav" class="block-content" style="display:block">
        <?php
        $currentCategory = Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId());
        Mage::register('current_category_parent_ids', $currentCategory->getParentIds());
        $rootCategory = Mage::getModel('catalog/category')->load(Mage::app()->getStore()->getRootCategoryId());
        echo getTreeCategories($rootCategory, false);
        ?>
    </div>
</div>
 
 
 
<?php
/**
 * recursive function that builds category tree
 * it'll only load children and siblings of the currently active category
 * @param $parentCategory
 * @param $isChild
 * @param int $level
 * @return string
 */
function getTreeCategories($parentCategory, $isChild, $level = 0) {
    $helper = Mage::helper('catalog/category');
    $parentCategoryIds = Mage::registry('current_category_parent_ids');
    $parentChildren = $parentCategory->getChildrenCategories();
    $class = ($isChild) ? "sub-cat-list" : "cat-list";
    $html = '<ul class="'.$class.'">';
 
    if($parentChildren->getSize()) {
        foreach($parentChildren as $category)
        {
            $html .= '<li class="level' . $level;
            if($category->getId() == Mage::registry('current_category')->getId()) {
                $html .= ' active';
            }
            if((in_array($category->getId(), $parentCategoryIds)) || $category->getId() == Mage::registry('current_category')->getId()) {
                $html .= ' active-parent';
            }
            $html .= '"><a href="' . $helper->getCategoryUrl($category) . '"><span';
            if($level == 0) {
                $html .= ' class="toplevel"';
            }
            $html .= '><div class="sub">' . trim($category->getName(), '- ') . '</div></span></a>';
            $subcats = $category->getChildren();
            if($subcats != '') {
 
                //drill down to show currently selected category, or show currently select category's children
                if((in_array($category->getId(), $parentCategoryIds)) || $category->getId() == Mage::registry('current_category')->getId()) {
                    $html .= getTreeCategories($category, true, ++$level);
                    --$level;
                }
            }
            $html .= '</li>';
        }
    }
    $html .= '</ul>';
 
    return $html;
}

Leave a comment

Your email address will not be published. Required fields are marked *