Custom Range Filter for Year in Magento 2
As an experienced Magento developer, I often encounter requests like yours where clients need to add custom filters, such as range filtering for the year attribute in their product catalogs. In this post, we’ll dive into the steps required to achieve this and address the challenges you’re currently facing.
Understanding the Current Approach
You’ve already made some progress by redefining the Magento class in your module (FullText\Collection) and updating the search_request.xml file. Your code snippet shows that you can filter products based on a specific SKU using the ‘in’ condition type:
$skus = [
'CNS334',
'U012840'
];
$this->filterBuilder->setField('sku');
$this->filterBuilder->setValue($skus);
$this->filterBuilder->setConditionType('in');
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
This works perfectly for filtering products based on a list of SKUs. However, you’re now facing a challenge when trying to join another table (make_year) and filter products based on their year attribute.
The Challenge: Joining Another Table
Your current approach involves joining the ‘my’ table using the following code:
$this->getSelect()->join(
[
'my' => 'make_year',
],
'e.entity_id = my.product_id'
);
This join statement seems correct syntactically, but you’re not able to get any results for your filtering. This could be due to a few reasons:
- The ‘year’ column in the ‘my’ table might have a different name.
- There might be no products associated with the specified year range.
- The join condition (‘e.entity_id = my.product_id’) might not be accurate.
Solving the Filtering Issue
To solve this issue, let’s break down your filtering code:
$this->searchCriteriaBuilder->addFilter($this->filterBuilder
->setField('year')
->setValue(2014)
->setConditionType('from')
->create()
);
$this->searchCriteriaBuilder->addFilter($this->filterBuilder
->setField('year')
->setValue(2015)
->setConditionType('to')
->create()
);
$this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
You’re adding two filters here: one for the ‘from’ year and another for the ‘to’ year. This approach is incorrect because Magento doesn’t support range filtering directly on custom attributes like this.
The Correct Approach
To filter products based on a year range, you should use the searchCriteriaBuilder to create a new search criteria object and then add your filters. Here’s an example of how to do this:
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchCriteria->setCurrentPage(1);
$searchCriteria->setPageSize(20);
$filterGroup = $this->filterGroupBuilder->create();
$fromYearFilter = $this->filterBuilder
->setField('year')
->setValue(2014)
->setConditionType('gteq') // greater than or equal to
->create();
$toYearFilter = $this->filterBuilder
->setField('year')
->setValue(2015)
->setConditionType('lteq') // less than or equal to
->create();
$filterGroup->addFilter($fromYearFilter);
$filterGroup->addFilter($toYearFilter);
$searchCriteria->setFilterGroups([$filterGroup]);
// Now you can use the $searchCriteria object for your search
In this example, we’re setting up a filter group with two filters: one for the ‘from’ year using the ‘gteq’ condition type and another for the ‘to’ year using the ‘lteq’ condition type. This ensures that only products within the specified year range are filtered.
Conclusion
Adding custom filters in Magento 2, such as a range filter for the year attribute, requires a deep understanding of the search criteria builder API and filter conditions. By following the approach outlined above, you should be able to successfully join your ‘my’ table and filter products based on their year attribute within the desired range.
Remember to test your code thoroughly after making these changes to ensure that the filters are working as expected.
If you’re looking for more advanced Magento 2 customization services or expert guidance, feel free to reach out to us at Laravel Company. Our team of experienced developers is ready to help you with any Magento projects you may have.