HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Product variants

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

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.

Get a product's variants

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

Code sample

public IEnumerable<ProductVariation> ListVariations(ContentReference referenceToProduct)
      {
        var relationRepository = ServiceLocator.Current.GetInstance<IRelationRepository>();
        var variations = relationRepository.GetChildren<ProductVariation>(referenceToProduct);
        return variations;
      }

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

Code sample

public IEnumerable<ProductVariation> GetProductByVariant(ContentReference variation)
      {
        var relationRepository = ServiceLocator.Current.GetInstance<IRelationRepository>();
        var products = relationRepository.GetParents<ProductVariation>(variation);
        return products;
      }

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

Code sample

public IEnumerable<ContentReference> ListParentProduct(EntryContentBase entryContent)
    {
        var parentProductLinks = entryContent.GetParentProducts();
        return parentProductLinks;
    }

Add 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.

Code sample

public void AddVariation(ContentReference referenceToProduct, ContentReference referenceToVariation)
      {
        var relationRepository = ServiceLocator.Current.GetInstance<IRelationRepository>();
        var newVariation = new ProductVariation
          {
            SortOrder = 100,
            Parent = referenceToProduct,
            Child = referenceToVariation
          };
        relationRepository.UpdateRelation(newVariation);
      }

Remove 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.

Code sample

public void RemoveVariation(ContentReference referenceToProduct, ContentReference referenceToVariation)
      {
        var relationRepository = ServiceLocator.Current.GetInstance<IRelationRepository>();
    
        // Define a relation matching the one to remove, or use
        // GetRelations to find the one you want to remove and pass that to
        // RemoveRelation
        var relationToRemove = new ProductVariation
          {
            Parent = referenceToProduct,
            Child = referenceToVariation
          };
    
        // Removes matching ProductVariation, or no action if no match exists
        relationRepository.RemoveRelation(relationToRemove);
      }