Showing posts with label Magento Category List. Show all posts
Showing posts with label Magento Category List. Show all posts

Wednesday, 19 October 2016

Display Magento Category on any page or custom module page

How you can get list of all categories of your Magento Store.


If you want to display all categories in homepage or any CMS page or any custom module page. There are different ways to get the category listing. Some method are below:-


Get all Categories:

The below code will fetch all categories (both active and inactive), which are added in your Magento Store.


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


Get all active categories


The below code will fetch all active categories, which are added in your Magento Store. Thus filtering the inactive categories.


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



Get active categories of any particular level


The below code will fetch all active categories of  particular level. Here, I have selected level 2.


$categories = Mage::getModel('catalog/category')->getCollection()
->addIsActiveFilter()
->addAttributeToFilter('level','2')
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url')
->addAttributeToSelect('is_active');


Now, You can display Name, URL, id etc. by following code:


foreach ($categories as $category)
{
        $entity_id = $category->getId();
        $name = $category->getName();
        $url_key = $category->getUrlKey();
        $url_path = $category->getUrl();
}