Share via


How to Get Products

Most products belong to a category. You can get these products as child products of the category. To do this, use the ChildProducts property on the Category object.

Your catalog may also contain root products; that is, products that are not in any category. To get these products, use GetRootCategory().ChildProducts. This property returns a collection of root-level products. You can use the CategoryConfiguration parameter to specify search options.

When displaying a product, you typically want to include some details about that product. Use the Information property on the CatalogItem object to get these details.

To get the products in a category

  1. Use the ChildProducts property on the Category object to access each product in the category.

  2. Use the Information property on the CatalogItem object to get additional information about the product.

To get a single product in a category

  1. Create a ProductConfiguration object to specify the search criteria.

  2. Use one of the GetProduct methods on the ProductCatalog object to get the specified product.

Example

This example demonstrates how to get a product from a category in a catalog. It describes how to create a CategoryConfiguration object that specifies the search criteria. It then uses those criteria to get a category. The example then iterates through the products in the category to access each one and write the display name to the console. It also gets the price for each product.

The example also shows how to get a specific product from the category. It creates a ProductConfiguration object to specify the returned information. It gets the specified product and its price.

private static void GetProducts(Catalog catalog, string categoryName, string productId)
{
    // Get the products in a category.
    // Create a CategoryConfiguration object to specify the search criteria.
    CategoryConfiguration categoryConfiguration = new CategoryConfiguration();
    categoryConfiguration.LoadChildProducts = true;
    categoryConfiguration.ChildProducts.SearchOptions = new CatalogSearchOptions();
    categoryConfiguration.ChildProducts.SearchOptions.ClassTypes = CatalogClassTypes.ProductClass | CatalogClassTypes.ProductFamilyClass;
    categoryConfiguration.ChildProducts.SearchOptions.SetPaging(1, 20);

    // Get a category to search.
    Category category = catalog.GetCategory(categoryName, categoryConfiguration);

    // Iterate through the products in the category.
    foreach (Product product in category.ChildProducts)
    {
         // Display the name of the product.
         Console.WriteLine(product.DisplayName);

         // Use the Information property to get additional information about the product.
         decimal price = product.Information.CatalogItems[0].ListPrice;
    }

    // Get a specific product in the category.
    // Create a ProductConfiguration object and specify search criteria.      
    ProductConfiguration productConfiguration = new ProductConfiguration();
    productConfiguration.InventoryOptions = new InventoryOptions();
    productConfiguration.InventoryOptions.FilterOutOfStockSkus = true;

    // Get the product as CatalogItem.
    CatalogItem catalogItem = catalog.GetProduct(productId, productConfiguration);
      
    // Get additional information about the product.
    decimal listPrice = catalogItem.Information.CatalogItems[0].ListPrice;
}

See Also

Other Resources

How to Browse a Catalog

Managing Products and Categories by Using the Catalog API