Product Relations
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 is 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 are interested in learning more about configurable products, read the following articles on how to work with configurable products.
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 are going to navigate to a product, go to the "Related Products" section and click the "New" button as shown below.
You should see the following dialog where we have to pick a product relation type and whether it is a one-way or a two-way relations. Having two-way relations means that the two relations are made for each product picked, one from the product you are on to the other product and another one from the other product back to the main product.
On the next dialog we have to pick which products we want to create the product relations between. In this example I have picked three products that are similar 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 "Add" button and we should see the following:
Getting Relations in the Frontend
Getting related products in the frontend can be done either through the index, or through this API:
EntitiesV2.Product.GetRelatedProducts(ProductRelationType)
Code Sample
Constructor setup with constructor injection:
private Ucommerce.Api.ICatalogContext CatalogContext { get; set; } private Ucommerce.EntitiesV2.IRepository<Ucommerce.EntitiesV2.Product> ProductRepository { get; set; } private Ucommerce.EntitiesV2.IRepository<Ucommerce.EntitiesV2.ProductRelationType> ProductRelationTypeRepository { get; set; } public ProductRelations(Ucommerce.Api.ICatalogContext catalogContext, Ucommerce.EntitiesV2.IRepository<Ucommerce.EntitiesV2.Product> productRepository, Ucommerce.EntitiesV2.IRepository<Ucommerce.EntitiesV2.ProductRelationType> productRelationTypeRepository) { CatalogContext = catalogContext; ProductRepository = productRepository; ProductRelationTypeRepository = productRelationTypeRepository; }
Using the Product Index will get you all related products to this product, regardless of ProductRelationType. This will return a list of guids. Here is a code snippet for that:
public void FetchRelatedProductsFromCurrentProduct() { Ucommerce.Search.Models.Product indexedProduct = CatalogContext.CurrentProduct; IList<Guid> relatedProductsFromIndex = indexedProduct.RelatedProducts; }
Using the EntitiesV2 to get the relations, you can use ProductRelationType, to get related products based on the specific type. This will return a dictionary of products, and types. Here is a code snippet for that:
public void FetchRelatedProductsFromSku() { var product = CatalogContext.CurrentProduct; var entitiesV2Product = ProductRepository.Select(x => x.Sku == product.Sku).First(); var relationType = ProductRelationTypeRepository.Select(x => x.Name == "Similar Products").First(); Dictionary<Ucommerce.EntitiesV2.ProductRelationType, List<Ucommerce.EntitiesV2.Product>> relatedProducts = entitiesV2Product.GetRelatedProducts(); Dictionary<Ucommerce.EntitiesV2.ProductRelationType, List<Ucommerce.EntitiesV2.Product>> relatedProductsOfSpecificType = entitiesV2Product.GetRelatedProducts(relationType); }
The GetRelatedProducts(...) methods both return a dictionary of the type Dictionary<ProductRelationType, List<Product>>
. If you do not 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".
Summary
In this article you 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 relation for a product or only product relations of a specific product relation type.