Magento Price Filter: From X to Unlimited
In Magento, filtering products by price is a common requirement. While the built-in functionality allows you to filter within specific ranges (e.g., $10-$20), it doesn’t directly support filtering for prices starting from an arbitrary amount with no upper limit.
This post will guide you through implementing this feature in your Magento store.
Understanding the Requirement
You want to allow customers to filter products by a minimum price, with the maximum value being unlimited. For example, users should be able to select “Price: $20 and above” without specifying an upper bound.
The Default Layered Navigation
By default, the layered navigation in Magento only supports filtering within closed ranges (e.g., $10-$20). It cannot handle open-ended ranges like “$20 and up”.
To achieve this, we’ll need to modify the way prices are filtered at the database level and update the front-end interface.
Database Modifications
Step 1: Create a New Price Index
Magento uses indexes for filtering products. To support our new filter, create a new index that includes only the minimum price of each product:
CREATE INDEX min_price_index ON catalog_product_entity (minimal_price) USING BTREE;
This index will allow us to quickly find all products with a minimum price greater than or equal to a given value.
Step 2: Update the Price Filter Query
In your custom module, override the get_filtered_products method in the layered navigation model (Magento\LayeredNavigation\Model\FilterableDataProvider::get_filtered_products). Modify the price range query as follows:
$priceRange = $this->getPriceRange();
if ($priceRange) {
$from = str_replace('$', '', $priceRange['from']);
if (is_numeric($from)) {
$joinCondition = "e.entity_id IN (SELECT e1.entity_id FROM catalog_product_entity AS e1
INNER JOIN catalog_product_index_min_price AS p1 ON p1.product_id=e1.entity_id
WHERE p1.min_price>=$from)";
$collection->getSelect()->joinLeft(array('inner_join' => 'catalog_product_entity'), "e.entity_id = inner_join.entity_id", array())
->where($joinCondition);
}
}
This code joins the catalog_product_index_min_price table to ensure we are filtering based on the minimum price of each product.
Front-end Changes
You’ll also need to update the layered navigation form in your theme to include a new “Price” field that only allows customers to enter a minimum value:
- Update the layered navigation template (e.g.,
vendor/magento/module-layered-navigation/view/frontend/templates/layer/filter.phtml) to add a text input for the price. - Use JavaScript to validate the input and ensure it’s not empty when the form is submitted.
You can find more detailed instructions for these front-end changes in our comprehensive guide on customizing layered navigation in Magento.
Conclusion
Implementing this “from X to unlimited” price filter requires a combination of database modifications and front-end customization. By creating a new index for the minimum price and updating the query and form, you can provide customers with more flexible filtering options.
Remember that implementing these changes directly in your production code is not recommended. Always test them thoroughly in a development environment first.
At Laravel Company, we specialize in custom Magento development and can help you implement complex features like this one while ensuring the stability of your e-commerce platform.