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.
The below code will fetch all categories with both active and inactive on your magento store
$categories = Mage::getModel('catalog/category') ->getCollection() ->addAttributeToSelect('*');
The below code will fetch all active categories that are present in your Magento Store.
$categories = Mage::getModel('catalog/category') ->getCollection() ->addAttributeToSelect('*') ->addIsActiveFilter();
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');
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);
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; ?>
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; ?>
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…