Multiple Layered Navigation Filters in Magento 1.7
Magento’s layered navigation is a powerful feature that allows customers to filter products based on various attributes such as price, color, size, etc. By default, this is placed in the left sidebar of the product listing page. However, there may be cases where you want to display multiple instances of the layered navigation on the same pageâfor example, placing one in the header and another in the sidebar.
Attempting to do this by simply adding the relevant block type to your XML layout file (e.g., adding <block type="catalog/layer_view" template="catalog/layer/view.phtml"/> to both head blocks) might seem like a straightforward solution. However, you’ll likely encounter an error whenever you click on a filter.
The reason behind this issue is that Magento internally uses the correlation name of each filter block to manage and process them. When you try to add more than one layered navigation block with the same template and type, it confuses Magento during the filtering process.
The Underlying Problem
When you click on a filter in one of your blocks, Magento attempts to join this data with the existing select query used for the other block(s). Since both blocks have the same correlation name (which is derived from the block type), Magento throws an error because it cannot differentiate between them during this operation.
Here’s a simplified version of the error message you might encounter:
You cannot define a correlation name 'device_idx' more than once
The stack trace that follows usually points to the Varien_Db_Select class where the join operation is failing due to the duplication of the correlation name.
The Solution: Customizing the Layered Navigation
To resolve this issue, you need to create a custom layered navigation block with its own template and correlation name. Here’s how you can achieve this:
-
Create a new template: Copy the
catalog/layer/view.phtmlfile from your Magento installation directory and save it as something likecustom_layer_view.phtmlin your theme folder (e.g.,app/design/frontend/your_package/your_theme/template/catalog/layer/). -
Update the XML layout: In your new template, locate the following line:
<block type="catalog/layer_view"/>
Replace it with a unique block identifier and give it a new correlation name:
<block type="core/text_list" name="custom_layer">
<block type="catalog/layer_view" template="catalog/layer/custom_layer_view.phtml"/>
</block>
- Update your theme’s layout XML: Add the custom layered navigation block to your desired position(s). For example, you can add it to both head blocks:
<default>
<reference name="head">
<block type="catalog/layer_view" template="catalog/layer/view.phtml"/>
</reference>
<reference name="left">
<block type="core/text_list">
<action method="addLink" linkId="custom_layer"></action>
</block>
</reference>
</default>
- Clear the cache: After making these changes, clear your Magento cache to apply them.
Conclusion
While it is technically possible to display multiple instances of the layered navigation in a single page using the default block type and template, this approach can lead to errors when users try to filter products. The key is to create a unique custom layered navigation block with its own template and correlation name.
By following the steps outlined above, you should be able to successfully display multiple layered navigation filters on your Magento 1.7 site without encountering any issues during the filtering process.
Remember that working with layout XML files and creating custom templates requires a good understanding of Magento’s architecture and development practices. If you’re new to Magento development, consider learning more about its structure and best practices from resources like Laravel Company.