w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to Display the categories in tree structure

This will help you to fetch you category tree

<?php
$rootcatId= Mage::app()->getStore()->getRootCategoryId();
$categories =
Mage::getModel('catalog/category')->getCategories($rootcatId);
function  get_categories($categories) {
    $array= '<ul>';
    foreach($categories as $category) {
        $cat =
Mage::getModel('catalog/category')->load($category->getId());
        $count = $cat->getProductCount();
        $array .= '<li>'.'<a
href="'.Mage::getUrl($cat->getUrlPath()). '">'.        
$category->getName() . "(".$count.")</a>
";
        if($category->hasChildren()) {
            $children =
Mage::getModel('catalog/category')->getCategories($category->getId());
             $array .=  get_categories($children);
            }
         $array .= '</li>';
    }
    return  $array . '</ul>';
}
echo  get_categories($categories); ?>




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.