Persistent Layered Navigation Filters in Magento
Magneto’s layered navigation is a powerful feature that allows customers to filter products based on various attributes like color, size, price, etc. However, by default, when you select an option (like a specific color), the selected attribute disappears from the filter list. This can be confusing for users and isn’t always the desired behavior.
Understanding the Default Behavior
In Magento’s core layered navigation implementation, the filters are designed to show only the available options that match the current product set. Once you select an option, the other options that no longer apply are hidden. This is a built-in feature that cannot be easily disabled through the admin interface alone.
A Clean Solution: Customize the Layered Navigation Block
To keep your selected filters visible in the navigation, we need to override Magento’s core layered navigation block and modify its behavior. Here’s how you can achieve this:
-
Create a custom theme if you haven’t already.
-
Navigate to
app/code/Magento/LayeredNavigation/Block/Filter.php. -
Copy this file into your custom theme under
app/code/Vendorname/YourModule/Block/LayeredNavigation/Filter.php. -
Open the copied file and find the
_getItemsDatamethod, which is responsible for preparing the filter options. -
This is where we introduce our change. We need to ensure that all selected items remain in the list regardless of availability. To do this, comment out or remove these lines from the method:
if ($block->isFilterInUrl() && $item->getValue()) {
continue;
}
- Now, let’s add a check for our own custom filter.
We’ll use Magento’s event system to apply our modification only when we want it.
- Open
app/code/Magento/Theme/etc/events.xmland add the following events:
<event name="catalog_layer_get_filter_items">
<observer name="custom_lazy_filter" instance="Vendorname\YourModule\Observer\LayerFilter" />
</event>
- Create a new observer.
In app/code/Vendorname/YourModule/etc/config.xml, add the following:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<type name="Vendorname\YourModule\Observer\LayerFilter">
<arguments>
<argument name="filter" xsi:type="array">
<item name="color" xsi:type="boolean">true</item> <!-- Change to true for the filter you want -->
</argument>
</arguments>
</type>
</config>
- Implement the observer.
Create app/code/Vendorname/YourModule/Observer/LayerFilter.php:
namespace Vendorname\YourModule\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
class LayerFilter implements ObserverInterface
{
protected $_filter;
public function __construct(
\Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter
) {
$this->_filter = $filter;
}
public function execute(Observer $observer)
{
if ($this->_filter->getName() == 'color') { // Change to your filter name
$items = $observer->getItems();
foreach ($items as &$item) {
$item->setIsAvailable(true); // Set every item as available
}
return $items;
}
}
}
Conclusion
By following this approach, you can keep the selected filter options visible in Magento’s layered navigation even after selection. This provides a cleaner user experience and makes your site more intuitive to navigate.
Remember, this is a custom solution that requires development skills. It involves modifying core Magento files, which should be done with caution. Always backup your files before making changes and test thoroughly.
For more advanced techniques in customizing Magento’s layered navigation, consider exploring the Laravel Company’s guide on Magento extensions.
This approach allows you to keep your site performant while providing a better user experience. It’s a practical solution that demonstrates how to leverage Magento’s event system for extensibility.
Happy coding!