Magento: Implementing Custom Price Range Filter
In the latest versions of Magento, the price filter has been enhanced to allow for more precise product selection based on fixed price slabs. While this provides a structured approach to filtering, you might find that it doesn’t fully meet your requirements for allowing users to select a custom range between a minimum and maximum price.
To achieve this level of granularity, we’ll need to implement a custom price filter that accepts the min_price and max_price parameters. Let’s dive into the steps required to make this happen.
Understanding the Default Price Filter
By default, Magento uses the price parameter with values representing the slab numbers (1 for 0-1000, 2 for 1000-2000, etc.). This is a fixed structure that doesn’t allow for open-ended ranges specified by the user.
Creating a Custom Price Filter
To begin, we need to create a new module that will handle the custom price filter functionality. In your project’s app/code directory, generate a new module named vendor_name/Magento_PriceFilter.
Inside this module, navigate to etc/frontend/di.xml and add the following configuration:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Url\Context">
<arguments>
<argument name="useSessionInUrl" xsi:type="number">1</argument>
</arguments>
</type>
</config>
This configuration allows the URL to include session data, which is necessary for our custom price filter.
Next, in etc/extension_attributes.xml, define the attributes we’ll use:
<extension_attributes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<attribute code="min_price" type="decimal" sort_order="999" />
<attribute code="max_price" type="decimal" sort_order="998" />
</extension_attributes>
Building the Filter
The core of our custom price filter will be a plugin for the Magento\Catalog\Model\Layer\Filter\AbstractFilter class. Create a file named abstract_filter_plugin.php in app/code/vendor_name/Magento_PriceFilter/etc/frontend/di.xml:
<type name="Magento\Catalog\Model\Layer\Filter\AbstractFilter">
<plugin name="vendor_name_price_filter" type="VendorName\MagentoPriceFilter\Plugin\Layer(AbstractFilter)" sortOrder="10"/>
</type>
Now, implement the Layer class with the plugin:
namespace VendorName\MagentoPriceFilter\Plugin;
class Layer
{
public function afterGetItems(Magento\Framework\App\RequestInterface $request, Magento\Catalog\Model\Layer $subject)
{
if ($request->getParam('min_price') && $request->getParam('max_price')) {
// Your price filtering logic here...
}
return $result;
}
}
Integrating the Filter with the Frontend
To display the custom price filter in your Magento frontend, you’ll need to modify your theme’s catalogProductList.phtml file.
Find the section where the default price slabs are rendered and replace it with your own HTML structure for a custom range input.
Conclusion
Implementing a custom price filter that accepts open-ended ranges requires a combination of module creation, configuration changes, and frontend modifications. By following the steps outlined above, you can provide your Magento users with more control over their product filtering experience by allowing them to specify their own minimum and maximum prices.
Remember, when making these changes, always test thoroughly in a development environment before deploying to production. And if you find yourself needing professional assistance or looking for pre-built extensions, consider visiting Laravel Company. They offer high-quality Magento services and solutions tailored to meet your specific business needs.
Happy coding!