In this tutorial, you will learn how to create associations between objects within Episerver Commerce... all using the power of code. In Episerver Commerce a product or variant can be associated with multiple nodes. This is really useful when you have requirements like creating a node to store all your discounted products, or, you sell products in multiple regions and the product may need to live under several country nodes. This type of association within Episerver Commerce is very different compared to an association within Episerver CMS. In the CMS, a page can only have one parent. When you are working with products within the Commerce catalogue, you can create as many associations between nodes and different products and variants as you would like. As Episerver Commerce works differently compared to CMS, it also means you will need to use a different API call. if you want to learn more about this API call, read on 🔥🔥🔥
ILinksRepository
In Episerver Commerce when we want to create an association you will be using the ILinksRepository
which can be found within EPiServer.Commerce.Catalog.Linking namespace. The code to use ILinksRepository
is shown below:
On Line1 I access ILinksRepository
. On Line6, I create a new object of type NodeRelation
. This object needs to be populated with two bits of data, the source (the product or variant) and the target (the node). A new association is created using the UpdateRelation()
method. Granted the naming of this method is not the best as UpdateRelation()
is used to update and add new relations. Adding a relationship is as simple as that 💥
If you run this code, within your catalogue you should now see the variant has a new relation:
*NOTE: When you add the code, the Target must always be a Node. You cannot create a node relation between two variants, or, products for example. Â If you try to set the variant as the target you will get the error below.
You now know how to create associations within Episerver Commerce. When you want to create an association in code use ILinksRepository
and the badly named 'UpdateRelation' property. Happy Coding 🤘