Creating variations

To create a product variation, you must perform the following:

  1. Get the catalog manager.
    Get an instance of the CatalogManager object.
  2. Get the specified product.
    Get an instance of the product that the variation will belong to. For more information, see Querying products.
  3. Get the specified attribute value.
    Get an instance of the attribute value that the variation will be associated to.
  4. Create new product variation.
    To create new product variation, call the CreateProductVariation method of the manager.
  5. Set the properties of the ProductVariation instance.
    In this example the following properties are set:
    • AdditionalPrice
    • Parent
    • Sku 
    • IsActive
  6. Set the Variant property.
    To set the variant property, you must perform the following:
    1. Create an instance of the AttributeValuePair class.
    2. Set its AttributeId property to the ID of the attribute that the specified value belongs to. 
    3. Set the AttributeValueId to the ID of the specified attribute value.
    4. Create an instance of the JavaScriptSerializer class.
    5. Serialize the AttributeValuePair instance into a string.
    6. Set the Variant property of the variation to the serialized string.
  7. Create a product variation detail.
    To create a product variation detail, you must perform the following:
    1. Create a new instance of the ProductVariationDetail class by calling the CreateProductVariationDetail method of the manager. 
    2. Set the ProductAttributeParent to the instance of the attribute that the specified attribute value belongs to.
    3. Set the ProductAttributeValueParent to the instance of the specified attribute value.
    4. Set the ProductVariationParent to the instance of the product variation.
    5. Set the ProductVariationDetailParentId to Guid.Empty.
  8. Add the variation to the product.
    Add the instance of the product variation to the ProductVariations collection of the product.
  9. Save the changes.
    Save the changes to the catalog manager.

Here is a code example:

public static void CreateProductVariation(Guid productId, Guid attributeValueId, string sku, decimal additionalCharge, bool isActive)
{
    CatalogManager catalogManager = new CatalogManager();
 
    Product product = catalogManager.GetProduct(productId);
 
    if (product == null)
    {
        return; //Product does not exist.
    }
 
    ProductAttributeValue attributeValue = catalogManager.GetProductAttributeValue(attributeValueId);
 
    if (attributeValue == null)
    {
        return; //Attribute value does not exist.
    }
 
    ProductVariation variation = catalogManager.CreateProductVariation();
 
    //Set the properties
    variation.AdditionalPrice = additionalCharge;
    variation.Parent = product;
    variation.Sku = sku;
    variation.IsActive = isActive;
 
    //Set the Variant property
    AttributeValuePair attributeValuePair = new AttributeValuePair();
    attributeValuePair.AttributeValueId = attributeValue.Id;
    attributeValuePair.AttributeId = attributeValue.Parent.Id;
 
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string attributeValuePairJson = serializer.Serialize(attributeValuePair);
 
    variation.Variant = attributeValuePairJson;
 
    //Create the product variation detail
    ProductVariationDetail detail = catalogManager.CreateProductVariationDetail();
    detail.ProductAttributeParent = attributeValue.Parent;
    detail.ProductAttributeValueParent = attributeValue;
    detail.ProductVariationParent = variation;
    detail.ProductVariationDetailParentId = Guid.Empty;
 
    product.ProductVariations.Add(variation);
 
    catalogManager.SaveChanges();
}

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK