Ucommerce includes full API reference documentation and lots of helpful articles to help you build your e-commerce site as effortlessly as possible.
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.
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:
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.
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.
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: