Remove empty categories from magento menu

Remove empty categories from magento menu

August 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Remove empty categories from magento menu

Removing Empty Categories from Magento Menu: A Step-by-Step Guide

As an e-commerce developer, you might find yourself wanting to clean up your main menu by removing any categories that are currently empty. While this is straightforward in the layered navigation using PHP code, it can feel challenging when working with the main menu in Magento.

Understanding the Main Menu Structure

The main menu in Magento is built within the Mage_Page_Block_Html_Topmenu class, specifically inside the _getHtml function. This function retrieves all the child categories and renders them into the HTML structure of the top menu.

When you try to access a category using $child->getId(), you will typically receive something like “category-node-36”. This string represents the node identifier for that specific category within the menu hierarchy.

Finding Your Way Around

Your intuition is correct: it should be possible to use getProductCount() to check if a category has any products assigned to it. However, directly incorporating this check into the existing _getHtml function can feel daunting due to its complexity and the need for class extension.

Before diving in, let’s first ensure that your categories are set up correctly:

  1. Ensure Products Are Assigned: Make sure that every category you want to display has at least one product assigned to it within the Magento admin interface.

  2. Check Category Visibility: Verify that the ‘Include in Navigation Menu’ setting is enabled for each visible category under Display Settings.

Getting Your Hands Dirty

To remove empty categories from the main menu, you’ll need to extend the Mage_Page_Block_Html_Topmenu class and override the _getHtml function. Here’s a step-by-step process:

  1. Create an Extension: If you haven’t already, create a custom extension that will handle this functionality. You can follow this guide on creating Magento extensions for a detailed walkthrough.

  2. Override the Topmenu Block: In your extension, locate the app/code/local/Your_Extension/Pagename/etc/config.xml file and add the following code to override the top menu block:

<blocks>
    <your_extension_pagename>
        <class>Your_Extension_PageName_Block</class>
    </your_extension_pagename>
    <your_extension_pagename_topmenu>
        <class>Your_Extension_PageName_Block_Topmenu</class>
        <type> models/specific </type>
        <instance>topmenu/block_html_topmenu</instance>
    </your_extension_pagename_topmenu>
</blocks>
  1. Create the Custom Topmenu Block: In your extension’s block folder (app/code/local/Your_Extension/Pagename/Block), create a new file called Topmenu.php. This will be your custom version of the top menu block.

  2. Override the _getHtml Function: Within the Topmenu class, override the _getHtml function to include your empty category check:

public function _getHtml()
{
    $html = '<ul class="level0 nav">';
    $children = $this->getChildren();
    $count = 0;

    foreach ($children as $child) {
        if ($child->getProductCount() > 0 && $child->isVisible()) { // Check for product count and visibility
            $html .= '<li class="level' . $count . ' nav-' . $child->getId() . '">';
            $html .= '<a href="' . $this->helper('core/url')->getUrl($child->getRequestPath()) . '" class="' . $this->getClass(' level0 ', $child) . '">' . $this->escapeHtml($child->getName()) . '</a>';
            // Continue with your original HTML structure
        }
    }

    $html .= '</ul>';
    return $html;
}
  1. Clear Cache: After making these changes, clear the Magento cache to ensure the new functionality takes effect.

Conclusion

Removing empty categories from the main menu in Magento involves a bit of class extension and overriding existing functions. By following this guide, you can create a custom top menu block that checks for product count and visibility before rendering each category node.

Remember, careful testing is essential when making changes to core Magento functionality. Always backup your data and test thoroughly on a staging environment first.

For more advanced Magento development topics, consider exploring our Magento Guides for in-depth tutorials and best practices from experienced developers.

Catalog Extensions Admin Configuration

Our network