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.
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; ?>