Friday, 12 December 2014

getting top category on the product listing or product details page

create a method in your helper:

<?php
class Rajiv_Catalog_Helper_Data extends Mage_Core_Helper_Abstract {
public function getTopCat(){
$category = Mage::registry('current_category');
$it = 10; //Amount of iterations before script gives up

if ($category) {
while($category->getLevel() != 2 && $it > 0) {
$category = $category->getParentCategory();

if (!$category) {
break;
}
}

if ($category) {
return $category->getName();
}
return;
}
}
}


----

Call the method anywhere as

echo Mage::helper('rajiv_catalog')->getTopCat();

No comments :

Post a Comment