I am trying to filter the category products collection on the basis of custom product attribute, i used many ways to override the collection but every method is filtering the records per page rather than getting all the matched product before paginating.
We are overriding this file: Magento\Catalog\Block\Product\ListProduct.php Method we are overriding: _getProductCollection()
This is the code reference:
public function _getProductCollection() { if ($this->_productCollection === null) { $this->_productCollection = $this->initializeProductCollection(); } $jayParsedAry = [ [ "attribute" => "custom_attribute", [ [ "eq" => "", [ "null" => true ] ] ] ], [ "attribute" => "custom_attribute", "finset" => "1111" ], [ "attribute" => "custom_attribute", "finset" => "1112" ] ]; $this->_productCollection->addAttributeToSelect('*')->addAttributeToFilter($jayParsedAry); return $this->_productCollection; }
There are total 19 filtered records, but showing 109. On the first page, there are only 6 records showing, instead of 12 per page. Please suggest a proper way to implement filters.
Thanks.
Hello @jasvirkine5e95
To properly filter the category products collection based on a custom product attribute and ensure that all matched products are retrieved before paginating, you can modify the _getProductCollection() method in your custom class that overrides Magento\Catalog\Block\Product\ListProduct.
Here's the code that applies the attribute filters correctly:
public function _getProductCollection() { if ($this->_productCollection === null) { $this->_productCollection = $this->initializeProductCollection(); } $jayParsedAry = [ [ 'attribute' => 'custom_attribute', 'null' => true, ], [ 'attribute' => 'custom_attribute', 'finset' => '1111', ], [ 'attribute' => 'custom_attribute', 'finset' => '1112', ], ]; $this->_productCollection->addAttributeToSelect('*'); $this->_productCollection->addAttributeToFilter($jayParsedAry); $this->_productCollection->setPageSize(false); // Disable pagination return $this->_productCollection; }
The changes made include:
By setting the page size to false, you're instructing Magento to fetch all the matched products instead of paginating them. This will ensure that the collection contains all the filtered products before rendering the page.
Please note that disabling pagination in this manner can impact performance if you have a large number of products. Consider implementing a more efficient solution if pagination is necessary for handling a large product catalog.
Still not working. I added below code:
$jayParsedAry = [ [ "attribute" => "custom_attribute", "null" => true ], [ "attribute" => "custom_attribute", "eq" => "" ], [ "attribute" => "custom_attribute", "finset" => "1111" ], [ "attribute" => "custom_attribute", "finset" => "1112" ] ]; $this->_productCollection->addAttributeToSelect('*')->addAttributeToFilter($jayParsedAry); $this->_productCollection->setPageSize(false);
Results are the same as before.
It looks like you are trying to filter the product collection based on a custom product attribute in Magento. However, the issue you are facing seems to be related to pagination and displaying incorrect results. Let's go through the code and see if we can identify and resolve the problem.
1. The Code:
The code you provided seems to be attempting to filter the product collection based on the custom attribute "custom_attribute." The filter appears to be defined in the `$jayParsedAry` array.
2. Filter Definition:
Let's take a closer look at the `$jayParsedAry` array:
```php
$jayParsedAry = [
[
"attribute" => "custom_attribute",
[
[
"eq" => "",
[
"null" => true
]
]
]
],
[
"attribute" => "custom_attribute",
"finset" => "1111"
],
[
"attribute" => "custom_attribute",
"finset" => "1112"
]
];
```
It seems that the `$jayParsedAry` contains an array with three filter conditions. The first condition appears to check for "custom_attribute" being empty or null, while the second and third conditions use "finset" to match values "1111" and "1112" respectively.
3. Potential Issue:
The issue might be related to how the conditions in `$jayParsedAry` are combined. The way you have defined the array, it seems like the conditions are being combined using an "OR" logic. As a result, the collection might include products that match any of the three conditions.
If you want to filter the collection using "AND" logic, meaning that products must match all three conditions simultaneously, you should place all the conditions under a single array. Here's how you can adjust the `$jayParsedAry` for "AND" logic:
```php
$jayParsedAry = [
[
"attribute" => "custom_attribute",
[
["eq" => "", ["null" => true]],
["finset" => "1111"],
["finset" => "1112"]
]
]
];
```
4. Check Pagination Settings:
The pagination issue could be unrelated to the filtering. Make sure you have the correct pagination settings configured in your Magento backend. Navigate to `System > Configuration > Catalog > Catalog > Storefront > Products per Page` and set the desired value for "Products per Page" (e.g., 12).
After making these changes, clear the cache and test your product collection again. If the filtering and pagination are still not working as expected, consider checking for any other customizations or extensions that might be affecting the product collection in your Magento installation.