Last updated: Feb 09 2018

Area: Episerver Commerce Applies to versions: 11 and higher
Other versions:

Product variants

This topic describes how to work with product variants (also known as variations) in relation to the content model. 

How it works

The EPiServer.Commerce.Catalog.Linking.ProductVariation class represents a relation between a product and a variant. To administer those relations, use EPiServer.Commerce.Catalog.Linking.IRelationRepository service.

The Child property of the ProductVariation contains the ContentReference of the variant. The class also has a SortOrder property, which describes the order of the variants, and a GroupName property for grouping variants. The Quantity property is not used for variants. EPiServer.Commerce.Catalog.Linking.EntryRelation contains default values that can be used: DefaultGroupName and DefaultQuantity.

A ProductVariation is uniquely defined by the ContentReference in its Child property together with its Parent property (referencing the product that has the variant). That is, the same variant cannot be added to a product more than once.

Getting a product's variants

Call the GetChildren method of IRelationRepository with the ContentReference of a product to get the following variants.

To get the following products, call the GetParents method of ILinksRepository with the ContentReference of a variant:

Or, get a variant's parent product by using entry content extensions.

Adding a variant to a product

Use the UpdateRelation method or extension method of IRelationRepository to add new ProductVariation objects to a product. The new variant must have a Parent ContentReference and a Child ContentReference.

Removing variant from a product

To remove a variant from a product, call the RemoveRelation method or extension method of IRelationRepository, with a ProductVariation object matching an existing variant. You can either construct a matching object, or use GetParents to get the existing Relations, filter out the object you want to remove, and pass it to RemoveRelation.


My understanding is that the class EPiServer.Commerce.Catalog.Linking.ProductVariation represents the relationship between the Product and Variation. If this is correct, then I find the first sentence under "How It Works" confusing. The text

"The EPiServer.Commerce.Catalog.Linking.ProductVariation class represents product variants and administers them using the EPiServer.Commerce.Catalog.Linking.IRelationRepository service."

seems to imply that the ProductVariation class represents a catalog entity, instead of a relationship between two entities.

@Drew: Good point. We will update the documentation to reflect that better. 


In new Commerce there is no interface ILinksRepository. As I understand we should call RemoveRelation on the instance of IRelationRepository? If this is correct, section "Removing variant from a product" is little confusing because in code you are still using ILinksRepository.


ILinksRepository is still there. it is just obsoleted. You can/should use IRelationRepository instead. We will update the sample code. Thanks 

Sample code has been updated.

@Quan Mai
Yes its obsoleted, and (delated? as wrote in braking changes Commerce 12) and because of it, I cannot use it :) Also in newest documentation in class library there is no ILinksRepository included to EPiServer.Commerce.Catalog.Linking namespace as obsolete. Thanks  

I think right now it's still incorrect:

var relationRepository = ServiceLocator.Current.GetInstance<relationRepository>();

It should be:

var relationRepository = ServiceLocator.Current.GetInstance<IRelationRepository>();

@Bartlomiej: I stand corrected. ILinksRepository was obsoleted in Commerce 11 and is now removed :)  


I updated the code sample -- thanks for your suggestion.