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();
}


15 comments:

  1. This post is pretty well maintained and really nice post. Thanks for this and keep updating.
    Online Marketing Company Delhi

    ReplyDelete
  2. Pretty nice post and very impressive, thanks for this post, Keep updating.
    Self Balancing Hoverboard Price

    ReplyDelete
  3. Really impressive post and very well maintained.
    KetoArt

    ReplyDelete
  4. Really amazing post, thanks for it and keep updating.
    hoverboard india

    ReplyDelete
  5. This coding is really necessary for the display category.
    Ubilom

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete