How to determine if a product is a variant?
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!
-
- Posts: 38
- Joined: 10 Oct 2024, 12:51
How to determine if a product is a variant?
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:
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?
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';
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?
Re: How to determine if a product is a variant?
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:
It's not recommended to use a custom SQL query. If you want to check that in any Aimeos class, use instead:
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.
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>'
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 );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,
