Get all category with child in Magento.

<hr>
<ul style=”margin-left: 15px;”>
<?php
$obj = new Mage_Catalog_Block_Navigation();
$storeCategories = $obj->getStoreCategories();
Mage::registry(‘current_category’) ? $currentCategoryId = Mage::registry(‘current_category’)->getId() : $currentCategoryId=”;
foreach ($storeCategories as $_category):
?>
<li>
<strong><?php echo $_category->getName(); ?></strong>
<?php $categoryChildren = $_category->getChildren(); ?>
<?php if($categoryChildren->count()) : ?>
<ul>
<?php foreach($categoryChildren as $_categoryChild) : ?>
<?php $_categoryChildModel = Mage::getModel(‘catalog/category’)->load($_categoryChild->getId());?>
<?php $categoryGrandchildren=$_categoryChild->getChildren(); ?>
<li>
<?php
$currentCategoryId === $_categoryChild->getId() ? $bold=”style=\”font-weight:bold\”” : $bold=”;
echo ‘&emsp;’ . ‘<a href=”‘ . $_categoryChildModel->getUrl() . ‘”‘ . $bold . ‘>’ . $_categoryChild->getName() . ‘(‘ . $_categoryChildModel->getProductCollection()->count() . ‘)</a>’;
?>
</li>
<?php if($categoryGrandchildren->count()) : ?>
<?php foreach($categoryGrandchildren as $_categoryGrandchild) : ?>
<?php $_categoryGrandchildModel = Mage::getModel(‘catalog/category’)->load($_categoryGrandchild->getId());?>
<li>
<?php
$currentCategoryId===$_categoryChild->getId() ? $bold=”style=\”font-weight:bold\”” : $bold=”;
echo ‘&emsp;&emsp;’ . ‘<a href=”‘ . $_categoryGrandchildModel->getUrl() . ‘”‘ . $bold . ‘>’ . $_categoryGrandchild->getName() . ‘(‘ . $_categoryGrandchildModel->getProductCount() . ‘)</a>’;
?>
</li>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach ?>
</ul>

Leave a comment