Solving Magento Active Filters on Search Page Issue
Hello Magento developers,
Today, we’re going to troubleshoot an issue that many of us have faced while implementing active filters in our e-commerce stores. The problem is simple: the filters work perfectly on category pages but fail miserably when it comes to the search page.
Before we dive into the solution, let me remind you that if you’re looking for professional Magento development services or need expert advice on your projects, feel free to reach out to us at Laravel Company.
Understanding the Problem
You’ve already shared the relevant code snippet that’s causing the trouble. The issue lies in the fact that the search page doesn’t use the same filter management system as the category pages do.
<?php /*Create filter dependencies*/
$_activeFilters[] = array();
$_filters = Mage::getSingleton('Mage_Catalog_Block_Layer_State')->getActiveFilters();
foreach ($_filters as $_filter):?>
<?php echo $this->stripTags($_filter->getLabel()) ?>
<a href="<?php echo $_filter->getRemoveUrl() ?>" title="<?php echo $this->__(âRemove This Itemâ) ?>"><?php echo $this->__(âRemove This Itemâ) ?></a>
<?php endforeach; ?>
This code is intended to display the active filters and provide a way to remove them. However, it relies on the Mage_Catalog_Block_Layer_State class which isn’t properly initialized when the search page loads.
The Solution: Overriding the Core File
To fix this issue, we need to override the core file that manages the filter state in Magento. This file is located at:
app/code/core/Mage/Catalog/Block/Layer.php
We’ll create a new file with the same name but in our custom theme directory:
app/design/frontend/[your_theme]/template/catalog/layer.phtml
Now, we need to make a few modifications to this file. We’re going to add a check for the current page being either ‘catalogsearch_result_index’ or ‘catalogsearch_advanced_result’. If it is any of these two pages, we’ll initialize our filter state manually.
<?php if ($this->getRequest()->getActionName() == 'index' && $this->getRequest()->getModuleName() == 'catalogsearch') : ?>
<?php $_activeFilters = Mage::getSingleton('Mage_Catalog_Block_Layer_State')->init($_productCollection) ?>
<?php endif; ?>
<?php /*Create filter dependencies*/
$_filters = isset($_activeFilters) ? $_activeFilters->getActiveFilters() : array();
foreach ($_filters as $_filter):?>
<?php echo $this->stripTags($_filter->getLabel()) ?>
<a href="<?php echo $_filter->getRemoveUrl() ?>" title="<?php echo $this->__(âRemove This Itemâ) ?>"><?php echo $this->__(âRemove This Itemâ) ?></a>
<?php endforeach; ?>
In this modified code, we first check if the current page is a search result page. If it is, we initialize our filter state using the init method of the Mage_Catalog_Block_Layer_State class.
After that, we proceed with displaying the active filters and the removal links as before.
The Result
After applying this change, you should see your active filters working perfectly on both category pages and search pages. The filters will now be displayed at the top of your search results page, allowing customers to refine their searches more effectively.
Remember, if you get stuck or need a professional Magento developer to help with your project, don’t hesitate to contact us at Laravel Company. We’re always here to assist you!
Happy coding!