Displaying Multi-level Categories in Magento
If you’re working with the popular e-commerce platform Magento, you may find yourself needing to display categories from multiple levels within your product pages or category listings. This is especially useful when you want to show all sub-categories of a parent category in your sidebar.
Understanding Your Category Structure
Let’s first understand the category structure you’ve shared:
Default
- Cat A
- Child A
- Child B
- Child C
- Child D
- Cat B
- Child A
- Child B
- Child C
In this hierarchy, Child A, Child B, etc., are direct children of Cat A, while Cat A and Cat B are both children of the default category.
Displaying Multi-level Categories
To achieve your goal, you’ll need to modify the way Magento retrieves and displays categories. By default, it only shows one level of sub-categories in the sidebar.
Here’s a step-by-step guide on how to display all sub-categories at any depth:
- Override the Category Listing Template
First, let’s override the category listing template file (catalog_category_view.xml) located under app/code/core/Mage/Catalog/etc folder.
<layout version="0.7.0">
<upgrade>
<!--- Override catalog_category_view template -->
<handler module="catalog" method="catalogCategoryView">
<action method="setTemplate" translate="false">
<argument name="template" xsi:type="string">your_custom_template.phtml</argument>
</action>
</handler>
</upgrade>
</layout>
Replace your_custom_template.phtml with the path to your custom category listing template.
- Create Your Custom Template
Create a new file in app/design/frontend/[your_package]/[your_theme]/template/catalog/category/view.phtml.
- Modify the Category Tree Creation Logic
In your new view.phtml file, locate the following PHP block that creates the category tree:
$tree = $this->getLayout()->createBlock('catalog/navigation')->getTree();
Replace it with this custom logic to retrieve all sub-categories at any depth:
// Get current category id
$currentCategoryId = Mage::registry('current_category')->getId();
// Fetch all child categories of the current category
$categories = Mage::getModel('catalog/category')
->getCollection()
->setStoreId(Mage::app()->getStore()->getId())
->addAttributeToSelect('name')
->addAttributeToFilter('parent_id', array('eq' => $currentCategoryId))
->addIsActiveFilter()
->load();
// Create the category tree
$tree = new Varien_Data_Tree();
foreach ($categories as $category) {
$node = new Varien_Data_Tree_Node($category->getId(), $category);
$tree->addNode($node, 'root');
}
- Display the Custom Category Tree
Finally, display your newly created category tree using Magento’s standard core/text_list block:
<?php if ($this->getCurrentCategory()): ?>
<div class="block block-layered-nav">
<div class="block-title">
<strong><span><?= $this->__('Categories') ?></span></strong>
</div>
<div class="block-content">
<?php echo $this->getChildHtml('catalog.category.tree'); ?>
</div>
</div>
<?php endif; ?>
Conclusion
By following these steps, you should now be able to display all sub-categories of a parent category at any depth in your Magento store. This can significantly improve the navigation experience for your customers.
Remember that modifying core files is generally discouraged in Magento development. Consider using an extension or creating your own custom module to achieve this functionality for better maintainability and upgrade compatibility.
For more advanced Magento theming and customization techniques, I highly recommend checking out Laravel Company’s Ultimate Guide to Magento Development. This comprehensive guide covers everything from installation to custom theme creation.