Ucommerce includes full API reference documentation and lots of helpful articles to help you build your e-commerce site as effortlessly as possible.

Topics Payment Providers

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, scroll to the "Relations" tab and click the "New" as shown below.


You should see the following dialog where we have to pick a product relation type, whether it is a one-way or a two-way relations and clicking next will reveal a search box, where you can search for the product you want to relate this product to. image


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 Search API. In the following example we're taking basis out of the CurrentProduct on CatalogContext).

Please note the related products property on the product is a total list of all the product relations available excluding their relation type.

    var currentProduct = Ucommerce.Infrastructure.ObjectFactory.Instance.Resolve<ICatalogContext>().CurrentProduct;
    ResultSet<Product> relatedProducts = Ucommerce.Infrastructure.ObjectFactory.Instance.Resolve<Ucommerce.Search.IIndex<Ucommerce.Search.Models.Product>>()
        .Where(p => currentProduct.RelatedProducts.Contains(p.Guid)).ToList();

The snippet above will load the related products from the Index. In case you're interested in only the relation types that makes sense, you can achieve this by a roundtrip to the SQL database to fetch the relations by it's type. Mind the performance here as SQL server queries are in general slower than loading strictly from the search index. In this case it is needed in case you need only the relation of a certain type.

    //key is the relation type name
    Dictionary<string, IEnumerable<Guid>> relationsDictionary = Ucommerce.EntitiesV2.ProductRelation.All().Where(relation => relation.Product.Guid == currentProduct.Guid).GroupBy(x => x.ProductRelationType.Name).ToDictionary(x => x.Key, y => y.Select(x => x.RelatedProduct.Guid));
    List<Guid> relationsByType = relationsDictionary["default"].ToList();
    ResultSet<Product> relatedProductsFromDictionary = Ucommerce.Infrastructure.ObjectFactory.Instance.Resolve<Ucommerce.Search.IIndex<Ucommerce.Search.Models.Product>>()
        .Where(p => relationsByType.Contains(p.Guid)).ToList();


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.