How to Rank Related Items
You can use ranking to control how items are displayed on your Web site. Ranking applies to both base catalogs and virtual catalogs. Items and relationships in a virtual catalog inherit their ranking from the base catalog. You can override the ranks in the virtual catalog.
It is possible to have the same ranking for two or more items. Items are first sorted by rank, then alphabetically. If you do not specify a rank for a relationship the sorting is alphabetical.
You can set the rank for related items when you create the relationship.
To set the rank on a related product
- Use the AddRelationshipToProduct method on the CatalogItem object to create the relationship and set the rank for it.
To edit the rank of a related product
Use the RelatedProducts property on the CatalogItem object to get a dataset that contains the related products for the specified product. Use this property to access the products related to either a product or a category.
Iterate through the dataset to access the individual relationships and products.
To set the rank on a related category
- Use the AddRelationshipToCategory method on the CatalogItem object to create the relationship and set the rank for it.
To edit the rank of a related category
Use the RelatedCategories property on the CatalogItem object to get a dataset that contains the related categories for the specified product. Use this property to access the categories related to either a product or a category.
Iterate through the dataset to access the individual relationships and categories.
Example
This example describes how to set and edit the rank for a product relationship. This example creates a new relationship, sets the rank to 1, and saves the changes. It then accesses the relationship and changes the rank for each item in the relationship.
public static void AddRelationshipRank(CatalogContext context, string catalogName, string productId, string relationship, string targetId)
{
// Create a product relationship with a rank.
// Get the catalog.
ProductCatalog catalog = (ProductCatalog)context.GetCatalog(catalogName, "en-US");
// Get the product for which to create a relationship.
CatalogItem catalogItem = catalog.GetProduct(productId);
// Create the relationship with a rank 1 and save the item.
catalogItem.AddRelationshipToProduct(catalog.Name, targetId, relationship, "Description of relationship", 1);
catalogItem.Save();
// Change the rank.
int rank = 0;
CatalogRelationshipsDataSet relatedProducts = catalogItem.RelatedProducts;
foreach (CatalogRelationshipsDataSet.CatalogRelationship relatedProduct in relatedProducts.CatalogRelationships)
{
// Set the rank for all products in the relationship.
relatedProduct.Rank = ++rank;
}
// Save the changes.
catalogItem.Save();
}