Developer forum

Forum » Dynamicweb 10 » Product - master, single or variant?

Product - master, single or variant?

Kim Søjborg Pedersen
Reply

How do I best distinguish whether a product is a "Master" product, i.e. whether it has variants, is a single product, or whether it is a variant. As a query expression and with C#.

I've solved it myself but not in a particularly elegant way, so I'm asking how you do it?

Thanks.


Replies

 
Aleksandar Borislavov Ivanov Dynamicweb Employee
Aleksandar Borislavov Ivanov
Reply

Hi Kim,

I'm not sure what you mean when you say "as a query expression". 

This is the way the type is determinted internally based on the Product.Type

switch (product.Type)
{
case ProductType.Stock:
{
if (!string.IsNullOrWhiteSpace(product.VariantId))
{
pm.Kind = ProductDataModelKind.Variant;
return;
}
 
var hasVariantGroups = Services.VariantGroups.GetProductRelations(product.Id).Any();
if (hasVariantGroups)
{
pm.Kind = ProductDataModelKind.Master;
return;
}
 
pm.Kind = ProductDataModelKind.StandAlone;
 
break;
}
 
case ProductType.Service:
pm.Kind = ProductDataModelKind.Service;
break;
 
case ProductType.Bom:
pm.Kind = ProductDataModelKind.Bundle;
break;
 
case ProductType.GiftCard:
pm.Kind = ProductDataModelKind.GiftCard;
break;
}
 
Kim Søjborg Pedersen
Reply

Hi Aleksandar,
I should probably be more specific, I see, because what you describe is a product.type and it doesn't say anything about whether a product is a variant, single or master.
One of the use cases I have is: On Swift 2 you have a quickbuy and it is based on a product query. Unfortunately, this query includes master products, so that you can add a master product to the cart. I must have found a solution for that and thought it would be obvious to make another query where I avoid master products.

But I also often need to be able to distinguish between Master, variant and single in C# so thanks for the code example

 

in the attachment you can see what i mean when I say "as a query expression". 

Screenshot_2026-03-13_110928.png
 
Aleksandar Borislavov Ivanov Dynamicweb Employee
Aleksandar Borislavov Ivanov
Reply
This post has been marked as an answer

I see what you mean now. You could extend the index to include a custom field to store the ProductDataModelKind and make it accessible via query expressions, which is also a good candidate for a feature request.

Alternatively, you'll have to play around with the existing IsVariant, ExtendedVairantCount and other variant fields to mimic the C# code example checks and get the query result to produce only masters, variants and singles.

Votes for this answer: 1
 
Kim Søjborg Pedersen
Reply

Yes i PIM (Products) the Type column can be "Single" and "Master" so in that context Product type is at totally different meaning then Product.Type (Stock, Service, BOM, Gift Card....)

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Kim,
In a query you can play around with a few existing properties, either hardcoded or based on a parameter:

And you can override the default value of the parameter in your module.

In C# you should probably check the VariantId property of the product. If it has a value, it is a variant. Master should normally have VariantId empty.
And you may also be able to compare the VariantId with the DefaultVariantID to determine if you see the default Variant of a product.
I hope it helps.

Adrian
 

 

You must be logged in to post in the forum