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

Product variants

Recommended reading 

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.

Do you find this information helpful? Please log in to provide feedback.

Last updated: Feb 09, 2018

Recommended reading