Magento - How to Set Category in Layered Navigation
The layered navigation feature is a powerful tool built into Magento that allows shoppers to filter products based on various attributes such as price range, brand, color, size, and category. By default, the layered navigation displays only the main category when you search for a product.
To display sub-categories in your layered navigation, you need to modify the way the filters are generated. Let’s dive into the code provided and see what needs to change.
Understanding the Default Filter Code
The provided code snippet:
<dl id="narrow-by-list">
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>
<?php if($_filter->getName() != "Category"){ ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd>
<?php echo $_filter->getHtml() ?>
</dd>
<?php } endif; ?>
<?php endforeach; ?>
</dl>
Gets the list of filters and then iterates through each filter. It checks if a filter has items (products) in it and if its name isn’t “Category”. If both conditions are met, it displays the filter’s name as a header (<dt>) followed by the HTML for that filter’s options (<dd>).
Setting the Current Category Programmatically
The code you tried:
<?php
$layer = Mage::getSingleton('catalog/layer');
$_categ = Mage::getModel('catalog/category')->load(5);
$layer->setCurrentCategory($_categ);
?>
Attempts to set the current category to the one with an ID of 5. However, this code is placed outside any template file and isn’t executed during the normal rendering process.
The Solution: Overriding the Layered Navigation Block
To achieve your goal, you need to override the layered navigation block in your theme. This allows you to modify how the filters are displayed without changing the core Magento files directly.
- Create a new template file
In your theme directory (e.g., app/design/frontend/{your_package}/{your_theme}/template/catalog/layer/view.phtml), create a copy of the original view.phtml file and name it something like view_override.phtml.
- Add the Category Sub-filter
Open your new template file (view_override.phtml) and find the following lines:
<?php if($_filter->getName() != "Category"){ ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd>
<?php echo $_filter->getHtml() ?>
</dd>
<?php } endif; ?>
Replace them with:
<?php if($_filter->getName() == "Category"){ ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd>
<?php echo $_filter->getHtml(); ?>
<!-- New Category Sub-filters -->
<?php foreach ($_filter->getItems() as $_category): ?>
<?php if($_category->getLevel() > 1): ?>
<ul class="subcategories">
<li><a href="<?php echo $this->getUrl('catalog/category/view', array('id' => $_category->getId())) ?>"><?php echo $this->__($_category->getName()) ?></a></li>
</ul>
<?php endif; ?>
<?php endforeach; ?>
</dd>
<?php } endif; ?>
- Update the Layout XML
Now, you need to update your theme’s layout file (app/design/frontend/{your_package}/{your_theme}/layout/catalog.xml) to apply this new template:
<catalog_category_layered translate="label">
<reference name="layered navigation">
<action method="setTemplate"><template>catalog/layer/view_override.phtml</template></action>
</reference>
</catalog_category_layered>
- Clear the Cache
After making these changes, clear your Magento cache to see the updates.
Conclusion
By overriding the layered navigation block in your theme and adding a few lines of code, you can now display sub-categories in your layered navigation. This approach ensures that your customization is applied only to your specific theme without affecting other themes or the core Magento files.
Remember, it’s always recommended to use child themes for any modifications to avoid losing your changes during updates. For more information on how to create and maintain Magento themes, I recommend checking out our guide on How to Create a Custom Magento Theme.
Happy coding!