Categories: Magento

How To Get All Categories in Magento.?

Get All Categories in Magento
When you need to display all categories on homepage or any cms page then you can use below methods to work with different list of all Magento Categories, all active Magento Categories, get specific categories, Store Specific Categories, Top level categories, and Subcategories only for the Current Top Category for using them in your E-Commerce platform.


These are the following different ways to get the category listing on homepage or cms pages.

Magento Get All Categories Active and Inactive:

The below code will fetch all categories with both active and inactive on your magento store

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

Magento Get All Active Categories:

The below code will fetch all active categories that are present in your Magento Store.

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

Magento Get Active Categories To Any Particular Level(Specific Category):

The below code will fetch all active categories of specific level and sorting categories by name.

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

Magento Get Store Specific Categories:

The below code will fetch active store specific categories

getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)

$helper = Mage::helper('catalog/category');

// sorted by name, fetched as collection
$categoriesCollection = $helper->getStoreCategories('name', true, false);

// sorted by name, fetched as array
$categoriesArray = $helper->getStoreCategories('name', false, false);

Magento Get All Top Level Categories and SubCategories:

The below code will fetch all the Top Level Categories as well as All Subcategories for your current magento store.

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?> //Top Level Category Listing
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> 
//Sub Category Listing
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Magento Get Subcategories only for the Current Top Category:

The below code will fetch all the Subcategories only for the Current Top Category.

<?php $_currentCategory = Mage::register('current_category') ?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>" title="<?php echo $_category->getName() ?>">
<?php echo $_category->getName() ?>
</a>
<?php if ($_category->getId() == $_currentCategory->getId()): ?>
<?php $_subcategories = $_currentCategories->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>" title="<?php echo $_subcategory->getName() ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Rohan pathak

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

1 year ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago