Category Tree Magento

Hello all,

I am listing here the method to display Category Tree, you can further modify the script for getting products etc.

This can be written in any phtml file, but its a better practice to make a model for this.

<?php
$cat_mod = Mage::getModel('catalog/category');
$_main_categories=$this->getStoreCategories();
if ($_main_categories):
    foreach ($_main_categories as $_main_category):
      if($_main_category->getIsActive()): 
      	   $cid = $_main_category->getId();
           $cur_category = $cat_mod->load($cid);
           $category_name = $cur_category->getName();
           echo '-'.$category_name.'<br/>';           
           res($cur_category,'-');
      endif;
    endforeach;       
endif;    

function res($cur_category,$s)
{
	$children_categories = $cur_category->getChildrenCategories();
	if(!empty($children_categories))
	{
		$s .= $s;
		foreach($children_categories as $k => $v)
		{
			$all_data = $v->getData();
			$nm = $all_data['name'];						
			echo $s.$nm.'<br/>';
			res($v,$s);
		}
	}
}
?>

Hope this is helpful.

One thought on “Category Tree Magento

Leave a Reply

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

Scroll to top