Cant filter producst by multiple categories
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Cant filter producst by multiple categories
Hi, Im having some issue, that i can not filter products by multiple categories, for example i have 10 items in category A and 3 in category B, I want to show items only which are in both A and B.
I was trying to add some code to vendor/aimeos/ai-controller-frontend/src/Controller/Frontend/Product/Standard.php on aimeos 2023.10
But none was working for me, no products was given back, while there should be at least 2.
or this one:
$ids are = [130, 79].
This line is commented out:
//$this->addExpression( $this->filter->compare( '==', 'index.catalog.id', $ids ) );
Thanks!
I was trying to add some code to vendor/aimeos/ai-controller-frontend/src/Controller/Frontend/Product/Standard.php on aimeos 2023.10
But none was working for me, no products was given back, while there should be at least 2.
Code: Select all
$expr = [];
foreach($ids as $catid) {
$func = $this->filter->make('product:has', ['index.catalog.id', 'default', $catid]);
$expr[] = $this->filter->compare( '==', $func, 0 );
}
$addFilter = $this->filter->and($expr);
$this->addExpression($addFilter);
Code: Select all
$this->addExpression( $this->filter->and( [
$this->filter->compare( '==', 'index.catalog.id', $ids[0] ),
$this->filter->compare( '==', 'index.catalog.id', $ids[1] )
] ) );
This line is commented out:
//$this->addExpression( $this->filter->compare( '==', 'index.catalog.id', $ids ) );
Thanks!
Re: Cant filter producst by multiple categories
By default, categories are OR combined if you hand over a list of IDs. Changing this to a AND combination is a bit more difficult due to how relational databases work.
First, you have to add a new search function to the product manager by creating a decorator (only add getSearchAttributes()):
https://aimeos.org/docs/2023.x/models/e ... /#easy-way
The search function must be counting the records is a sub-select like this one:
https://github.com/aimeos/aimeos-core/b ... #L288-L297
Then, you can use that search function in your custom product frontend controller. Extend from the Standard class in your own extension, overwrite the category() method there and configure the new class name:
- https://github.com/aimeos/ai-controller ... #L227-L254
- https://aimeos.org/docs/2023.x/config/c ... duct/#name
First, you have to add a new search function to the product manager by creating a decorator (only add getSearchAttributes()):
https://aimeos.org/docs/2023.x/models/e ... /#easy-way
The search function must be counting the records is a sub-select like this one:
https://github.com/aimeos/aimeos-core/b ... #L288-L297
Then, you can use that search function in your custom product frontend controller. Extend from the Standard class in your own extension, overwrite the category() method there and configure the new class name:
- https://github.com/aimeos/ai-controller ... #L227-L254
- https://aimeos.org/docs/2023.x/config/c ... duct/#name
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,
