Creating a Range Filter for Custom Attributes in Magento
When it comes to filtering products by custom attributes like weight in Magento, you might encounter challenges when trying to implement range filters similar to those used for price. This post will guide you through solving this issue and providing a working example of a weight range filter.
The Problem: Weight Range Filter Not Working
You’ve attempted to replicate the functionality of the price range filter (e.g., domain/rings?price=20000-30000) for your custom attribute ‘weight’, but the filter isn’t working as expected. When you try to apply a weight range like domain/rings?weight=-400-500, Magento fails to recognize it.
Understanding the Issue
The problem lies in how Magento handles and indexes numeric attributes compared to price attributes. Magento uses the Z Sheridan algorithm for indexing prices, which allows for more complex filtering operations. For custom numeric attributes like weight, the standard Zend_Search_Lucene index is used, which doesn’t handle range queries as efficiently.
The Solution: Override the Product Collection
To solve this issue, you’ll need to override the product collection and manually apply a SQL WHERE clause to filter products by your custom weight attribute. Here’s a step-by-step guide:
-
Create an observer for the catalog_product_collection_load_after event.
In your
etc/events.xmlfile, define the following configuration:<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="catalog_product_collection_load_after"> <observer name="custom_weight_range_filter" instance="Vendor\ModuleName\Model\Observer\ProductCollectionWeightFilter" shared="false"/> </event> </config> -
Create the Observer class to filter products.
In your module, create a
ProductCollectionWeightFilterobserver:namespace Vendor\ModuleName\Model\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Event\Observer; use Magento\Catalog\Model\ResourceModel\Product\Collection; use Magento\Framework\App\Request\Http as Request; use Magento\Store\Model\StoreManagerInterface; class ProductCollectionWeightFilter implements ObserverInterface { protected $request; protected $storeManager; public function __construct( Request $request, StoreManagerInterface $storeManager ) { $this->request = $request; $this->storeManager = $storeManager; } public function execute(Observer $observer) { $collection = $observer->getEvent()->getCollection(); if (!$collection instanceof Collection) { return; } $weightRange = $this->request->getParam('weight'); if ($weightRange && is_string($weightRange)) { list($minWeight, $maxWeight) = explode('-', $weightRange); // Apply the SQL WHERE clause $collection->addAttributeToFilter( 'weight', ['from' => (double)$minWeight, 'to' => (double)$maxWeight] ); } } } -
Clear and reindex your catalog search index.
After implementing this observer, make sure to clear and reindex the catalog search index using
bin/magento indexer:reindex catalogsearch_fulltext.
Conclusion
By overriding the product collection and manually applying a SQL WHERE clause for your custom weight attribute, you can successfully implement range filtering in Magento. This approach bypasses the limitations of the standard Zend_Search_Lucene indexing used for numeric attributes.
Remember to replace Vendor\ModuleName with your actual module name and namespace. Also, ensure that your module is enabled and properly installed.
For more advanced customizations and a comprehensive guide on working with Magento events and observers, feel free to explore our blog posts at https://laravelcompany.com.