In today's guide, you will learn how to use the Episerver Commerce API to get information about a SKUs parent product.  If you need to get the product or the node associated with a variant then this is the guide for you 🔥🔥🔥

I'll be using this catalogue structure as the basis of this tutorial:

Getting Parent Products and Nodes For a Variant in Episerver Commerce 1

With the following product and variant:

Getting Parent Products and Nodes For a Variant in Episerver Commerce 2

ParentLink

In Episerver Commerce every variant will have a parent. The variant, can be one of three types, NodeContent, ProductContent and VariationContent. All of these types inherit from IContent and each type contains a number of properties that you would expect to find within a CMS page. If you use a tool like DotPeek on any of these types, you will see a familiar property, ParentLink that is inherited from CatalogContentBase. Each of these three types works slightly differently, however, they all contain the ParentLink property.

If you have more experience with Episerver CMS, you normally get information about a parent item usingParentLink. In Commerce, the `ParentLink works slightly differently. Let us see how...

In the catalogue structure defined above, we have a node, with a product that has a variant. On the variant, if you called its ParentLink method I think it's fair to expect that you would get back a reference to the product, however, you won't 🤔

Instead, you will get a reference to the NodeContent that the variant belongs in, e.g. its parent. In Episerver Commerce, the ParentLink will return the parent folder rather than the 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, however, it does work differently compared to how the CMS behaves. Carry on reading to find out why...

GetNodeRelations()

To get a reference to the product, you can use the GetNodeRelations() method on any product/variant. Calling GetNodeRelations() will return a list if catalogue node of all the varients related productswithin the catalogue tree:

The reason Episerver needs an API like GetNodeRelations() is that a variant might be associated with one or more products. The problem with a single property like 'ParentLink' is that this only returns a single value. In the Episerver Commerce structure, a variant can be associated with more than one product. This type of relationship might not make sense in your project and your variants may only need to be related to one product, however, it is an option. In these circumstances, the cleanest code to get the product is to use FirstOrDefault(), like so:

On any object of type VariationContent, you should see 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 catalogue, you can simply use FirstOrDefault() and ignore the rest 💥

ParentEntries()

If you are struggling to get the data you need with GetParentProducts(), your next step is to try and use ParentEnties. If you have a variant and you want to know about the products that it is related to, you can use the ParentEntries property (found in EntryContentBase). An example of how to use ParentEntries is shown below:

SearchManager()

If you have more complicated requirements and the example above do not work for your situation, you can always query the Commerce catalogue directly using CatalogEntrySearchCriteria. Using CatalogEntrySearchCriteria you can search the commerce catalogue against properties including, CatalogNames, CatalogNodes, ClassTypes, Currency, EndDate, FuzzyMinSimilarity, Locale, MarketId, Outlines, RecordsToRetrieve, and StartDate. The code to search for a product would look like this:

Using SearchManager and the ClassTypes, you can query the catalogue anyway you fancy. Handy!


You now know how to search for product information in Episerver Commerce like a BOSS. The relationships in a Commerce catalogue are more complex compared to the CMS. This is why there are multiple APIs that you can use in order to get the data you need. If your requirements are more complex than a simple parent mapping, you should look into using SearchManager. Happy Coding 🤘