Cant filter producst by multiple categories

How to configure and adapt Aimeos based shops as developer
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!
rmdaniel
Posts: 4
Joined: 03 Feb 2025, 09:08

Cant filter producst by multiple categories

Post by rmdaniel » 03 Feb 2025, 09:14

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.

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);
or this one:

Code: Select all

			 $this->addExpression( $this->filter->and( [
			 	$this->filter->compare( '==', 'index.catalog.id', $ids[0] ),
			 	$this->filter->compare( '==', 'index.catalog.id', $ids[1] )
			 ] ) );
$ids are = [130, 79].

This line is commented out:
//$this->addExpression( $this->filter->compare( '==', 'index.catalog.id', $ids ) );

Thanks!

User avatar
aimeos
Administrator
Posts: 8460
Joined: 01 Jan 1970, 00:00

Re: Cant filter producst by multiple categories

Post by aimeos » 04 Feb 2025, 13:48

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
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply