How to determine if a product is a variant?

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!
randomdennis
Posts: 38
Joined: 10 Oct 2024, 12:51

How to determine if a product is a variant?

Post by randomdennis » 25 Mar 2025, 12:37

I want to identify whether a product is a variant to hide it in a custom search query.

I tried this query, but it doesn’t work in all cases. Sometimes, there are no entries here, and if there are entries, they are not necessarily variants:

Code: Select all

SELECT * FROM `mshop_product_list`
WHERE `refid` = 4416
AND `domain` = 'product'
AND `type` = 'default';
I also tried checking mshop_index_attribute, but I didn’t find anything relevant:

The select product (7134) has no entries.

A variant (510) only has another attribute as an entry.

I searched for variant relationships elsewhere but couldn’t find any clear connection.

How else can I reliably determine whether a product is a variant or find all variants of a select product? Where is this relationship stored?

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

Re: How to determine if a product is a variant?

Post by aimeos » 26 Mar 2025, 08:33

Your query is almost correct, you only have to check if the product is a selection product to determine if the default product is a variant article. The SQL query would be:

Code: Select all

SELECT p."id" FROM "mshop_product" AS p
JOIN "mshop_product_list" AS pl ON p.id=pl.parentid
WHERE p."type" = 'default'
	AND pl."domain" = 'product'
	AND pl."type" = 'default'
	AND pl.refid = '<product ID>'
It's not recommended to use a custom SQL query. If you want to check that in any Aimeos class, use instead:

Code: Select all

$manager = \Aimeos\MShop::create( $this->context(), 'product' );
$filter = $manager->filter()->add( 'product.type', '==', 'select' );
$filter->add( $filter->make( 'product:has', ['product', 'default', '<product ID>'] ), '!=', null );
$manager->search( $filter );
This returns all selection products which have the variant article assigned or none if the article isn't a variant article. Keep in mind that you shouldn't execute that query for every product because it will execute one query for each product.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply