Share via


How to Create a Category Relationship

You can use the Catalog API to create relationships with a category. You can create category-to-category relationships or category-to-product relationships. These relationships can be to products and categories in the same catalog, or in different catalogs. Relationships are widely used to "cross-sell" products. That is, you can define a relationship between a category and either a product or another category that identifies similarities between the two. For example, if you have two catalogs, each with a category of long-sleeved tee shirts, you could create a relationship named "Long-sleeved tee shirts" that relates the category shirts from one catalog to a category from another catalog.

You can create relationships between a single category and multiple target items, either products or categories. The relationshipName parameter does not have to be unique.

You can create multiple relationships between a single category and a single target item, either a product or a category. In this case the relationshipName must be unique.

To create a relationship to a category

To delete a relationship to a category

  1. Use the RelatedCategories property on the CatalogItem object to get a dataset that contains the related categories for the specified category. Use this property to access the categories related to either a product or a category.

  2. Iterate through the dataset to access the individual relationships and categories.

Example

This example adds a relationship "Long-Sleeved Shirts" between a category and a target category in another catalog. It then accesses the related categories and iterates through them, displaying the relationship name. It then deletes the relationship to the target categories.

private static void CreateandDeleteCategoryRelationships(CatalogContext context, ProductCatalog productCatalog, string categoryName, string targetCatalog, string targetCategory)
{
    // Create a relationship between a category and a target category 
    // in a target catalog.
    CatalogItem category = catalog.GetCategory(categoryName);
    category.AddRelationshipToCategory(targetCatalog, targetCategory, "Long-sleeved shirts", "All long-sleeved shirts");
    category.Save();

    // Get the categories related to this category.
    CatalogRelationshipsDataSet relatedCategories = category.RelatedCategories;
    foreach (CatalogRelationshipsDataSet.CatalogRelationship relatedCategory in relatedCategories.CatalogRelationships)
    {
        // Display the relationship name.
        Console.WriteLine(relatedCategory.RelationshipName);
        // Delete the relationships on this category.
        category.RemoveRelationshipToCategory(targetCatalog, targetCategory, relatedCategory.RelationshipName);
    }
    // Save the changes.
    category.Save();
}

See Also

Other Resources

How to Create a Product Relationship