Magento – How to get all active categories and subcategories in product listing page

categorymagento-1.9module

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.

I got answer from this question
But, i failed to get all parent items (subcategories and subcategories)

$path = $category->getPath();          
What i get is : 0(root)/2(Magento root)/444(products)

And it escapes all the middle subcategories. In my case it escaped Refrigerator/backbar fridges

Best Answer

this will show like breadcrum.

I know this is quite old, but I'd like to share my solution anyway as it doesn't override/clone any core files.

In your custom module add the following to your config.xml:

<config>
    ...
    <frontend>
        <events>
            <catalog_controller_product_init>
                <observers>
                    <breadcrumb_categorypath_product_init>
                        <type>singleton</type>
                        <class><Your Namespace>_<Your Module>_Model_Observer</class>
                        <method>fullBreadcrumbCategoryPath</method>
                    </breadcrumb_categorypath_product_init>
                </observers>
            </catalog_controller_product_init>
        </events>
    </frontend>
</config>

Create an Observer.php in /app/code/local///Model/

Add the following to your Observer.php:

class <Your Namespace>_<Your Module>_Model_Observer {
    public function fullBreadcrumbCategoryPath(Varien_Event_Observer $observer) {
        $current_product = Mage::registry('current_product');

        if( $current_product ) {
            $categories = $current_product->getCategoryCollection()->addAttributeToSelect('name')->setPageSize(1);
            foreach( $categories as $category ) {
                Mage::unregister('current_category');
                Mage::register('current_category', $category);
            }
        }
    }

I hope this will help you.

Related Topic