How To Automatically Create Association Between Objects in the Episerver Commerce Catalog

When you are working with content within the PIM, you may want to create multiple associations from the products and variants to different types of node.  Before we get into the code, I should probably mention that an association in Episerver commerce is different to the normal Episerver CMS navigation hierarchy structure.  In the CMS for example, a page can only have one parent.  In Episerver Commerce things work slightly differently.

A product or variant can be associated under multiple nodes.  This is really useful when you have requirements like, creating a central store for all your products and then having a discounted node section to apply discounts to products.  Another potential example could be on multi-region sites where a product may need to live under several country nodes to be applicable to be sold within that market.

As Episerver Commerce works slightly differently than CMS, it also means that the API call is slightly different than the standard CMS approach.

In Episerver Commerce when we want to create one of these associations we use the ILinksRepository within the EPiServer.Commerce.Catalog.Linking namespace.

var linksRepository = ServiceLocator.Current.GetInstance<ILinksRepository>();
var variantToCreateAssociationWith = new ContentReference();
var nodeToHostTheVariantAssociation= new ContentReference();
var linkedEntry = new NodeRelation()
{
SortOrder = 0,
Source = variantToCreateAssociationWith ,
Target = nodeToHostTheVariantAssociation,
};
linksRepository.UpdateRelation(linkedEntry);

In the snippet above, we created a Node relation that contains a link to the source (the game or variant) and the target (the node we want to associate the product or variant too).  Even though it is called UpdateRelation,  it’s the method you want to use to create a new association.  Simply add your node relation and it’s as simple as that.  In your catalog you should now see the variant added as a relation:

episerver_node_association_created

WORD OF CAUTION 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.

node_error

Conclusion

In today’s guide we’ve talked about the difference in the navigation trees between Episerver Commerce andEpiserver CMS.  In Episerver CMS we use a very classic page navigation structure where each item can only have one parent. Episerver commerce works slightly different as catalog items can have associations to multiple parents.

When we want to create an association in code we can use the ILinksRepository. To create a new association we need to use the ‘UpdateRelation’ property, simple add the child item into the source and the parent into the target and you should be all sorted.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *