Magento 3rd level Subcategories Menu

Magento 3rd level Subcategories Menu

August 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 3rd level Subcategories Menu

Adding Third Level Subcategories to Your Magento Menu

Adding a third level of subcategories to your Magento menu can greatly enhance the user experience by providing more organized navigation options. In this post, we’ll guide you through the process of extending your existing two-level category structure to include a third level.

Understanding the Current Code Structure

Before proceeding, let’s break down the provided code snippet that currently handles the first and second levels of categories:

<div class="left_content">
    <div class="menu">
        <?php $_helper = Mage::helper('catalog/category') ?>
        <?php $_categories = $_helper->getStoreCategories() ?>
        <?php $currentCategory = Mage::registry('current_category') ?>

        <?php if (count($_categories) > 0): ?>
            <ul id="menu">
                <?php foreach($_categories as $_category): ?>
                    <li class="sub">
                        <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></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) ?>">
                                            <?php echo $_subcategory->getName() ?>
                                        </a>
                                    </li>
                                ?>
                            </ul>
                        <?php endif; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>

</div>

This code retrieves the top-level categories using Mage::helper('catalog/category')->getStoreCategories(), then iterates through each category. For each top-level category, it loads its children and iterates through them to create a hierarchical list.

Adding a Third Level of Subcategories

To add a third level, we need to modify the code as follows:

<div class="left_content">
    <div class="menu">
        <?php $_helper = Mage::helper('catalog/category') ?>
        <?php $_categories = $_helper->getStoreCategories() ?>
        <?php $currentCategory = Mage::registry('current_category') ?>

        <?php if (count($_categories) > 0): ?>
            <ul id="menu">
                <?php foreach($_categories as $_category): ?>
                    <li class="sub">
                        <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></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) ?>">
                                            <?php echo $_subcategory->getName() ?>
                                        </a>

                                        <!-- Add third level categories here -->
                                        <?php $_third_level = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
                                        <?php $_third_subcategories = $_third_level->getChildrenCategories() ?>

                                        <?php if (count($_third_subcategories) > 0): ?>
                                            <ul>
                                                <?php foreach($_third_subcategories as $_third_subcategory): ?>
                                                    <li>
                                                        <a href="<?php echo $_helper->getCategoryUrl($_third_subcategory) ?>">
                                                            <?php echo $_third_subcategory->getName() ?>
                                                        </a>
                                                    </li>
                                                <?php endforeach; ?>
                                            </ul>
                                        <?php endif; ?>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        <?php endif; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>

</div>

In this modified version, we’re doing the same process as before but for a third level. We first load the second-level subcategory and then retrieve its children using getChildrenCategories(). If there are more than zero categories at this third level, we create another nested <ul> list to display them.

Conclusion

Adding a third level of subcategories to your Magento menu is a straightforward process that involves extending the existing category loading logic. By following the code example provided in this post, you can easily enhance your website’s navigation by providing users with more organized and detailed categories.

Remember, proper categorization can greatly improve the user experience by making products easier to find. If you need further assistance or want to learn more about Magento development best practices, feel free to explore our resources at Laravel Company.

Catalog

Our network