Hiding or Showing Filters In Certain Categories in magento

Hiding or Showing Filters In Certain Categories in magento

July 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Hiding or Showing Filters In Certain Categories in magento

Magento: Hiding Specific Filters in High-Level Categories

When managing a large e-commerce store on Magento, you may find that certain categories have an overwhelming number of filter options, which can negatively impact the user experience. In this post, we’ll explore how to hide specific attribute filters for high-level categories using XML layout updates.

The Challenge: Hiding Filters in Magento

Let’s consider your current scenario where you’re trying to hide a particular attribute filter (e.g., “Color”) from appearing in the left navigation menu of a high-level category. You attempted to use the following code snippet in the custom layout update file:

<reference name="em.catalog.leftnav">
    <action method="setData">
        <instruction>hide_attribute_code</instruction>
        <value>color</value>
    </action>
</reference>

However, you noticed that this approach didn’t hide the “Color” attribute filter from the category page. The issue lies in the fact that Magento uses a different layout handle for the main category listing compared to sub-categories.

Understanding Magento Layout Handles

To effectively manage which filters are visible or hidden within specific categories, it’s crucial to understand how Magento handles the display of attribute filters.

By default, Magento uses the catalog_category_layered layout handle to display the layered navigation (filters) in a category. However, when dealing with sub-categories, it employs the catalog_category_layered_nochildren handle instead.

Hiding Filters for High-Level Categories

To solve this issue and hide specific attribute filters in high-level categories, we need to create an observer that will listen for the event triggered when rendering the category layered navigation. This approach allows us to conditionally hide or show the desired attributes based on the current category’s level.

Implementing a Custom Layout Update Observer

  1. Create a new module: If you haven’t already done so, create a custom Magento module where you will implement your observer.

  2. Add an event listener: In your module’s etc/events.xml file, define an event listener that will be triggered when the layered navigation is rendered for a category:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_category_layered_nochildren_renderer">
        <observer name="custom_module_category_filter_visibility" instance="YourVendor\CustomModule\Observer\CategoryFilterVisibility" />
    </event>
</config>
  1. Create the observer: In your module’s Observer directory, create a new class named CategoryFilterVisibility:
<?php

namespace YourVendor\CustomModule\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Block\Product\Registry as ProductRegistry;
use Magento\Catalog\Model\Layer\FilterableAttributeProviderInterface;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;

class CategoryFilterVisibility implements ObserverInterface
{
    private $registry;
    private $filterableAttributeProvider;

    public function __construct(
        ProductRegistry $registry,
        FilterableAttributeProviderInterface $filterableAttributeProvider
    ) {
        $this->registry = $registry;
        $this->filterableAttributeProvider = $filterableAttributeProvider;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Implement your logic here to conditionally hide or show attribute filters based on the current category level
        // You can access the current category using $observer->getData('category')
    }
}
  1. Implement filter visibility logic: In the execute method of your observer, you can now implement the desired logic to hide or show specific attribute filters based on the current category’s level.

For example, if you want to hide the “Color” attribute for categories above a certain depth:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $category = $observer->getData('category');
    $depth = $category->getLevel();

    if ($depth > 2) {
        $attributeCode = 'color';
        $filterableAttributes = $this->filterableAttributeProvider->get filterableAttributes($category);

        foreach ($filterableAttributes as $key => $attribute) {
            if ($attribute->getAttributeCode() == $attributeCode) {
                unset($filterableAttributes[$key]);
            }
        }

        $layer = $observer->getLayout()->getBlock('catalog.category.layered.navigation');
        $layer->setFilterableAttributes($filterableAttributes);
    }
}
  1. Test and deploy: After implementing the observer, test your changes thoroughly in a development environment before deploying them to production. Make sure that the desired attribute filters are hidden from high-level categories while still being visible in sub-categories.

Conclusion

Hiding or showing specific attribute filters in Magento requires a deep understanding of the platform’s layout and event system. By implementing a custom observer, you can conditionally manage which filters are displayed based on the current category’s level.

Remember that managing attribute visibility is crucial for maintaining a user-friendly e-commerce experience, especially when dealing with large product catalogs organized into multiple categories.

For more advanced Magento customizations and development best practices, consider exploring our comprehensive guides at Laravel Company.

Catalog Themes Errors

Our network