Поделиться через


How to Add a Product

You can add a product to a base catalog by using one of the CreateProduct methods of the BaseCatalog object.

To add a product to a base catalog

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

  2. Use the CreateProduct method to add the product to the catalog.

Example

This example adds a product "Sierra Boot" to an existing category "Footwear" in an existing catalog named "Outdoor Equipment". The example creates the custom properties "Sole Pattern" and "Size" for this product.

public static void AddNewProduct(CatalogContext context)
{
    // Get the catalog.
    BaseCatalog baseCatalog = (BaseCatalog)CatalogContext.GetCatalog("Outdoor Equipment");
    try
    {
        // Create the custom properties for the product.
        CatalogProperty size = context.CreateProperty("Size", CatalogDataType.String, 250);
        CatalogProperty solePattern = context.CreateProperty("Sole Pattern", CatalogDataType.String, 250);
        
       // Create a product definition for the boots in the catalog. 
        CatalogDefinition productDefinition = context.CreateDefinition("Sierra Boot", CatalogDefinitionType.ProductDefinition);

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

        // Add the boot product to the Footwear category in the catalog.
        Product boot = baseCatalog.CreateProduct(productDefinition.Name, "IR0874", 59.95m, "Footwear");

        // Define the properties of the product and save.
        boot["Sole Pattern"] = "Diamond";
        boot["Size"] = "4-12";
        boot.Save();
    }
    catch (EntityAlreadyExistsException e)
    {
        Console.WriteLine(string.Format("Exception: {0}", e.Message));
    }
}

See Also

Other Resources

Managing Products and Categories by Using the Catalog API

How to Create a Base Catalog by Using the Catalog API