Share via


How to Add a Product Family

A product family represents a product and its related variants. You create a ProductFamily object the same way you create a Product object. The difference is the CatalogDefinition that you use to create them. If the definition includes variants, a ProductFamily is created. Even if a product in the catalog does not have variants, it is a product family if the definition with which it was created contains variant properties.

To create a product family

  1. Get the catalog to which you want to add the product.

  2. Create a CatalogDefinition for the product family, if you do not already have one.

  3. Use the CreateProduct method to create the product family and add it to the catalog.

  4. Use the AddVariant method to add a variant to the product family.

  5. Define the properties and variants of the product family.

  6. Save the information.

Example

This example creates a product definition "Unisex Hiking Pants" with a variant property "Color." It creates a product family based on that definition and adds the product family to the category "Pants" in the catalog.

public static void AddNewProductFamily(CatalogContext context, string catalogName, string productId, string variantId)
{
    // Get the catalog.
    BaseCatalog baseCatalog = (BaseCatalog)CatalogContext.GetCatalog(catalogName);
    try
    {
        // Create the custom properties for the product.
        CatalogProperty size = context.CreateProperty("Size", CatalogDataType.String, 250);
        CatalogProperty pocketType = context.CreateProperty("Pocket Type", CatalogDataType.String, 250);
        CatalogProperty color = context.CreateProperty("Color", CatalogDataType.String, 50);
        
       // Create a product definition for the pants in the catalog. 
        CatalogDefinition productDefinition = context.CreateDefinition("Unisex Hiking Pants", CatalogDefinitionType.ProductDefinition);

        // Add the properties to the product definition and save the definition.
        productDefinition.AddProperty(size.Name, DefinitionPropertyType.NormalProperty);
        productDefinition.AddProperty(pocketType.Name, DefinitionPropertyType.NormalProperty);
        productDefinition.AddProperty(color.Name, DefinitionPropertyType.VariantProperty);
        productDefinition.Save();

        // Add the product to the Pants category in the catalog.
        ProductFamily pants = (ProductFamily)baseCatalog.CreateProduct(productDefinition.Name, productId, 55.00m, "Pants");

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

        // Define the properties of the product.
        pants["Pocket Type"] = "Buttoned Flap";
        pants["Size"] = "4-12";

        // Add the variant properties. In this case, the variant 
        // describes the blue version of this product. 
        pants.Variants[variantId].DisplayName = "Blue";

        // Save the product pants.
        pants.Save();
        
    }
    catch (EntityAlreadyExistsException e)
    {
        Console.WriteLine(string.Format("Exception: {0}", e.Message));
    }
}

See Also

Other Resources

How to Add a Product