0
votes

My categories and subcategories hierarchy to a product listing page :

  • Products
    • Refrigerator
      • back-bar fridges
        • Product listing(here)

Now, what i'm trying to get is while i'm in product listing page i want to get all the parent categories and subcategories with respective links.

What i tried is :

$categories = Mage::getModel('catalog/category')->getCollection()
                            ->addAttributeToSelect('*')
                            ->addIsActiveFilter();

But this doesn't render in product listing page. So how can i get all the active categories and subcategories in product listing page their links that will take me to the respective category/subcategory page.

1

1 Answers

1
votes
<?php $currentCategoryId = $this->getLayer()->getCurrentCategory()->getId();
        $curentCategory = Mage::getModel('catalog/category')->load($currentCategoryId);
        $path = $curentCategory->getPath();
        // $path will return category IDs like "1/5/16/30/45" : Here, 1 is the main root category & 2 is the Default magento root category. After these, you will get the category Ids by hierarchy . 

        /*
        * Output "1/5/16/30/45" will have following hierarchy 
        *1-
        *  2-
        *    16-
        *      30-
        *        45
        */
        // later you can explode by slash(/) and go ahead with your further display logics
?>