In this article you will learn what product relations are and how to work with them. You will also be presented with some of the use cases where it's recommended to use product relations.
What are Product Relations
Product relations are either one-way or two-way relations between products, linked by a relation type. A product can have multiple product relations to other products and the product relations can be used in different contexts based on the relation type.
We have listed some of the more popular use cases:
- Product Bundles
- Configurable products
- Similar products
- Product X is often bought with product Y
- Customers who bought product X also bought product Y
If you're interested in learning more about configurable products, read the following articles on how to work with configurable products.
If you're interested in the last two use cases then you should take a look at the Inconceivable app which dynamically creates product relations based on orders in the system.
Product Relation Types
A product relation type is an entity used to group product relations. When using products relations for multiple use cases, you want to be able to distinguish the product relations based on which use case they belong to. You will have a product relation type for each use case and use it to distinguish which product relations belong to which use case.
Now we are going to look into how to create a new product relation type. The use case we are going to present is creating product relations between similar products for upselling opportunities and the product relation type will be named "Similar Products".
The first thing we need to do is navigate the to "Settings" section of Ucommerce, open the "Catalog" node and right click on the "Product Relations" node as shown below.
Once you click "Create" you will be asked to enter a name for the new product relation type and in this case it will be "Similar Products". After you click the create button, you will be presented with the view shown below. You have now successfully created a new product relation type.
How to work with Product Relations
Now we are going to use the newly created product relation type to create product relations. To do this we're going to navigate to a product, open the "Related products" tab and click the "Add new relations button" as shown below.
You should see the following dialog where we have to pick a product relation type, whether it's a one-way or a two-way relations and which products we want to create the product relations between. In this case, we're going to set the product relation type to "Similar Products" and I have picked three products that are similar to the Black and White Wonderland shirt. Having two-way relations means that the two relations are made for each product picked, one from the Black and White Wonderland shirt to the other shirt and another one from the other shirt to the Black and White Wonderland shirt.
Once we have picked how we want to create the product relations and the products that we want to create the relation between, we click the "Save" button and we should see the following:
Getting related products in the frontend can be done by using the following APIs either
CatalogLibrary.GetRelatedProducts(string sku, string relationTypeName = null)
CatalogLibrary.GetRelatedProducts(int productId, string relationTypeName = null)
In this example I'm going to use the first method and get the relations based on a product Sku and a relation type name.
var productRelationDictionary = CatalogLibrary.GetRelatedProducts("BWWMSFSS-LE", "Similar Products"); var products = productRelationDictionary[productRelationDictionary.Keys.First()];
The GetRelatedProducts(...) methods both return a dictionary of the type
Dictionary<ProductRelationType, List<Product>>. If you don't specify a relation type name then it will return all the product relations grouped by their product relation type. In this case, we only get the product relations with the product relation type "Similar Products".
In this article you've learned how to work with product relations and that:
- Product relation types group together product relations.
- Product relations can be either one-way or two-way.
- Ucommerce APIs support getting a product relations for a product or only product relations of a specific product relation type.