Share via


How to Get Product Variants

You can use the Catalog API to get all the variants of the products in the catalog. You can also get a specific variant and specify search criteria by using the VariantConfiguration object.

To get the product variants

  1. Check whether the product has variants. A ProductFamily object is a product that has variants.

  2. Get the variants for a product. To do this, use the Variants property on the ProductFamily object. This property returns a collection of the variants of the product.

  3. Iterate through the variants and display the information.

To get a single variant for a product

  1. Create a VariantConfiguration object that contains the search criteria.

  2. Use the GetVariant method on the ProductCatalog object to get the specified variant.

Example

This example describes how to get the variants for a specific product in the product catalog. It creates a ProductConfiguration object to return specific information about the product. It checks whether the product is a ProductFamily object and, if so, it gets all the variants for the product. The example iterates through the returned collection of variants and writes the display name to the console.

The example also demonstrates how to get a specific variant.

Private static void GetProductVariants(string productId, string variantId)
{
    // Create a ProductConfiguration object to hold the search criteria.
    ProductConfiguration productConfiguration = new ProductConfiguration();
    productConfiguration.InventoryOptions = new InventoryOptions();
    productConfiguration.InventoryOptions.FilterOutOfStockSkus = true;
    CatalogItem catalogItem = catalog.GetProduct(productId, productConfiguration);
    
    // Get the variant products for this product
    // If the product is a ProductFamily it has variants.
    if (catalogItem is ProductFamily)
    { 
        VariantCollection variantCollection = ((ProductFamily)catalogItem).Variants;
        foreach (Variant variant in variantCollection)
        {
            Console.WriteLine(variant.DisplayName);
        }
            
        // Get the variant with the specified variantId.        
        VariantConfiguration variantConfiguration = new VariantConfiguration();
        variantConfiguration.InventoryOptions.FilterOutOfStockSkus = true;
        Variant productVariant = catalog.GetVariant(productId, variantId, variantConfiguration);
    }
}

See Also

Other Resources

How to Get Products

Managing Products and Categories by Using the Catalog API