Share via


How to Add a Product Variant

A variant is a specific item that is grouped with related variants that together form a product. Variants usually vary from each other in one or more properties but each product variant in a product family is based on the same product definition. For example, if your catalog includes tee shirts, the different colors of the shirts would each be a product variant.

A product variant always includes a unique identifier, the variantId.

Before you add a product variant to a product, you must ensure that the product definition includes variants. For more information, see How to Create a Product Definition.

To add a variant to a product

  1. Use the AddVariant method on the ProductFamily object to add the variant to the product family.

  2. Use the GetVariant method on the ProductFamily object to retrieve the Variant object specified by the variantId parameter.

  3. Set the properties for the variant.

Example

This example demonstrates how to add a product variant to an existing product. This example assumes that the product definition contains the variant property. It gets the existing product from the catalog and adds the variant to the product family. It then sets the properties for the variant product.

private static void AddVariant(CatalogContext context, string variantId, string catalogName, string productId)
{

    // Get the product family from the catalog.
    BaseCatalog baseCatalog = (BaseCatalog)context.GetCatalog(catalogName);
    ProductFamily productFamily = (ProductFamily)baseCatalog.GetProduct(productId);

    // Add the variant to the product family. 
    productFamily.AddVariant(variantId);

    // Set the properties for the variant product.
    productFamily.Variants[variantId]["Color"] = "Green";       
}

See Also

Other Resources

How to Create a Property