Hiding Specific Categories from Layered Navigation on Search Results
Are you struggling to exclude certain categories from your layered navigation, even though “Show in Navigation” is set to no? You’re not alone! In this guide, we’ll walk through how to hide specific categories from appearing in the layered navigation on search results.
Understanding the Problem: Why Does This Happen?
When a category has been added to a product and its “Show in Navigation” setting is toggled off, it typically disappears from the layered navigation across your entire store. However, sometimes you might notice that this category still appears on specific search result pages.
This issue often arises when using third-party extensions or when there are complex configuration settings involved. The solution requires understanding how Magento’s layered navigation works and manipulating its behavior at a more granular level.
Step 1: Identify the Category
Start by identifying which category you want to hide from the layered navigation on search results pages. Make sure this category is indeed set to “No” under the “Show in Navigation” setting:
Admin Panel > Catalog > Categories [Select your category] > Display Settings
Step 2: Override the Layered Navigation Block
To achieve our goal, we’ll need to override the catalog layered navigation block. This allows us to manipulate which categories are displayed based on certain conditions.
First, create an override file for the catalog/layer/view.phtml template under your theme directory:
app/design/frontend/[Your Theme]/Magento_Catalog/templates/layer/view.phtml
Step 3: Modify the Layered Navigation Display
Open the newly created view.phtml file and locate the following code snippet, which is responsible for rendering the layered navigation items:
<ol id="filter-nav">
<?php foreach ($block->getFilters() as $filterModelName => $filter) : ?>
<li>
<h4><?php echo $this->escapeHtml(__($filter->getName())) ?></h4>
<!-- ... -->
</li>
<?php endforeach; ?>
</ol>
We’ll modify this code to filter out the specific category you want to hide. Add a conditional check inside the foreach loop:
<ol id="filter-nav">
<?php foreach ($block->getFilters() as $filterModelName => $filter) :
if ($filter->getCategoryId() != [Your Category ID]) : ?>
<li>
<h4><?php echo $this->escapeHtml(__($filter->getName())) ?></h4>
<!-- ... -->
</li>
<?php endif; ?>
<?php endforeach; ?>
</ol>
Replace [Your Category ID] with the actual ID of the category you want to hide.
Step 4: Flush Cache and Verify
After making these changes, flush your Magento cache to ensure the new settings take effect:
bin/magento cache:flush
Now, when you perform a search that would previously display the hidden category in layered navigation, it should no longer appear. The layer navigation will now exclude the specified category based on the condition we added.
Conclusion
Hiding specific categories from layered navigation on search results pages requires a combination of properly configuring category settings and overriding Magento’s core templates to apply custom filtering logic.
By following this guide, you’ve successfully learned how to exclude unwanted categories from your layered navigation based on your specific business needs. This approach demonstrates the power of Magento’s flexibility and extensibility.
Remember that when dealing with complex configuration scenarios or third-party extensions, it’s always a good idea to consult with experts in Magento development, such as those at Laravel Company.
Happy coding!