Hide categories that have no products magento

Hide categories that have no products magento

August 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Hide categories that have no products magento

Hiding Empty Categories Per Website in Magento

As a seasoned Magento developer, I often come across clients who want to manage their product categories independently for each site while sharing the same root category structure across all sites. In this post, we’ll tackle how to hide empty categories that have no products assigned specifically to a particular website.

The Challenge: Managing Categories Across Multiple Websites

Let’s start by understanding the problem space:

  1. You have multiple websites (Site1, Site2, and Main Site) sharing the same root category structure.
  2. Each product is added to its respective site and also appears on the Main Site.
  3. However, you want categories to appear only if they contain products specific to that website.

For example:

  • Site1 should show Category1 and Category2 because those are the categories with products assigned to it.
  • Site2 and Site3 should display Category2 and Category3 for the same reason.
  • The Main Site should list all visible categories across all sites, which in this case would be Category1/2/3.

A Developer’s Approach: Customizing Category Visibility

To achieve this level of granular control over category visibility per website without breaking upgrade compatibility, we’ll create a custom module with the following steps:

  1. Create a new Magento 2 module: Follow the official Magento documentation here to create a new module named HideEmptyCategoriesPerWebsite.

  2. Override the category collection: In your custom module, override the core Magento 2 module responsible for generating the category tree - Magento\Catalog\Model\Category. Create an observer that modifies the category collection before it’s rendered on each website.

    Your observer should filter out categories that have no products assigned to the specific website being viewed.

  3. Update the category tree generation: In your theme, override the template responsible for generating the category tree (catalog/category/tree.phtml). Use Magento’s website ID and check if the current website has any products in the category before displaying it.

    Here’s a simplified example of how you might do this:

    <?php foreach ($this->getCurrentCategory()->getChildrenCategories() as $_child): ?>
        <?php if (count($_child->getProductsInCategory()) > 0 || $this->_websiteId == $_child->getWebsiteIds()): ?>
            <li><a href="<?php /* @escapeNotVerified */ echo $block->getUrl($_child) ?>"><?= /* @escapeNotVerified */ $_child->getName() ?></a>
        <?php endif; ?>
    <?php endforeach; ?>
    

Upgrading Compatibility and Future-Proofing

To ensure your code can be easily upgraded with future Magento versions without breaking changes, follow these best practices:

  • Use the plugin mechanism to override core functionality rather than direct model overrides.
  • Keep your customizations minimal and focused on specific business requirements.
  • Regularly update your module to match changes in the core Magento 2 release notes.

Conclusion: Achieving Granular Category Control

With a thoughtful approach using a custom module, observers, and template overrides, you can maintain a shared root category structure across multiple websites while displaying only relevant categories for each specific site. By following this method, you’ll preserve upgrade compatibility and keep your codebase clean and manageable.

Remember to test thoroughly in different scenarios and browser combinations to ensure the solution works as expected across all use cases.

For more advanced Magento customizations and best practices, be sure to explore our comprehensive resources at Laravel Company. Happy coding!

Magento 2 Catalog Extensions Deployment

Our network