There maybe time that you need to get the product or the node that a variant is associated with. In today’s guide, we will talk about the Episerver commerce API, specifically, surrounding parent items. In today’s guide, we will use a dummy catalog set-up like this:

With the following product and variant:

ParentLink

When we work with Episerver CMS we are used to getting the parent page using ParentLink. In Episerver commerce we work with NodeContent, ProductContent and VariationContent. All of the types inherit from IContent and each type contains a number of properties that you would expect to find in a CMS page. If you use reflector you should be able to see one familiar property, ParentLink, defined in ‘CatalogContentBase’. Each type works slightly differently BUT they all have ParentLink properties.

If you use the CMS frequently, ParentLink may work differently than you expect. In the dummy catalog structure defined above we have a node, with a product that has a child/related variant. On the variant if you called it’s ParentLink method I think it’s a fair to expect that you would get back a reference to the product, but you won’t.

Instead you will get a reference to the NodeContent that the variant belongs in, e.g. it’s parent. In commerce ParentLink will return the parent folder the product or variant lives in rather than the variants parent product.

To reiterate, ParentLink does not relate to the product/variant hierarchy, it relates to the folder/node structure. There are many situations where you need to know this information so it’s a useful API call to know about BUT it does work different than how the CMS behaves. Carry on reading to find out why…

GetNodeRelations

Instead of using ParentLink, the recommended way of getting the catalogue nodes/categories that a product or variant is related is via the GetNodeRelations method on the product/variant. Calling GetNodeRelations will return the catalog node the product belongs to within the catalog tree.

GetParentProducts

When we get a variant the parent/child relationship might not be known. A variant might be associated to one or more products. The problem with a single property like ‘ParentLink’ is that a variant can potentially have many parents.

In a lot of projects, this variant to multiple product relationship won’t be needed and a variant will only be related to one product. In these circumstances, the easiest way of getting a product where all variant can only have one parent product can be seen below:

On VariationContent you should be able to find the ‘GetParentProducts’ method. This method will return all the products associated with your variant. If you are confident you have a one-to-one product/variant relationship in your catalog, you can simply use FirstOrDefault() on the variant to get it’s parent product.

ParentEntries

If you are struggling with GetParentProducts, your next stop is to try and use ParentEnties. If you have a have a variant and you want to know about the products that it is related to, you can use the ParentEntries property in EntryContentBase.

SearchManager

If you have more complicated requirements than the API calls above, you can always search commerce directly yourself using a CatalogEntrySearchCriteria. Using CatalogEntrySearchCriteria you can search the commerce catalog agaisnt properties like:

Conclusion

In today’s guide, we’ve covered the differences in the catalog hierarchy compared to a normal CMS hierarchy. In commerce we deal with NodeContent, ProductContent and VariationContent. The relationships between these types is a lot more compelx than a standard CMS page strucutre and objects like variants can have multiple parents.

In commerce we have two types of API calls when dealing with products/variants. One to get the folder/node structure a product/variant might live in and the other one being the product/variant relationship.

We’ve covered ParentLink which can be used to find the parent node an item lives in. We’ve also covered GetParentProducts() to get a variants parent. If that fails you could also try to use ParentEntries.

If your requirements are more complex than a simple parent mapping and you need to use your own filtering, then you should look into using SearchManager and CatalogEntrySearchCriteria.

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

MY EPISERVER BOOK

EPISERVER NEWSLETTER

Connect

Hi, I'm Jon, I write articles about creating and optimizing websites as well as making money from your online business. I am technical architect and technology fanatic by profession. You can find out more about me by signing up to my newsletter.