0
votes

I'm working on a website and I got the name of the parent category of a category. But how can I get the name of the main category which contain multiple subcategory. Like a main category "A" has "B" as sub category which inturn has "C" as subcategory, which as another subcategory called "D". I managed to get the name of the category "A" if the products are in category "B". But not getting about how to get the category name "A" if the products are in category "D". Please help in this. My code to get the parent name goes like this.

$currentCat = Mage::registry('current_category');
$parentId = Mage::registry('current_category')->getParentId();
$parent = Mage::getModel('catalog/category')->load($parentId);
$parentname = $parent->getName();

The above code will fetch parent category name of current category. But how to get the multi level parent name. Please help me on this. I'm newbie to magento.

1
I cant understand your question. Do you want to get RootCategory name?Bhavik Shah

1 Answers

1
votes
`
 $parentId = Mage::registry('current_category')->getParentId();
 $parent = Mage::getModel('catalog/category')->load($parentId);
 while($parentId != 0) {//check for root category
     $parent_cat_array[] = $parent->getName();
     $parentId = $parent->getParentId();
     $parent = Mage::getModel('catalog/category')->load($parentId);
 }

 print_r($parent_cat_array);
 `

Just iterate it with every parent till it reaches root category