Getting Parent Products and Nodes For a Variant/SKU in Episerver Commerce

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:

episervercommerce_api_explained

With the following product and variant:

episervercommerce_api_explained_product_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.

var referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();
var contentReference = referenceConverter .GetContentLink(code);
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
var variant = repository.Get<VariationContent>(contentReference);
var parentContentReference = variant.ParentLink;

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.

var variant = new VariationContent();
var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
foreach (var nodeRelation in variant.GetNodeRelations())
{
var catalogNode = repo.Get<NodeContent>(nodeRelation.Target);
} 

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:

var referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();
var contentReference = referenceConverter .GetContentLink(code);
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
var variant = repository.Get<VariationContent>(contentReference);
variant = variant .GetParentProducts().FirstOrDefault();

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.

var referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();
var contentReference = referenceConverter.GetContentLink("CODE_GOES_HERE");
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
var product = repository.Get<VariationContent>(contentReference);
var variants = product.ParentEntries;

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:

CatalogNames, CatalogNodes, ClassTypes, Currency, EndDate, FuzzyMinSimilarity, Locale, MarketId, Outlines, RecordsToRetrieve, StartDate.

A snippet of code that will search for a product with a given name would look like this

var criteria = new CatalogEntrySearchCriteria
{
SearchPhrase = "phrase",
ClassTypes =
new CommaDelimitedStringCollection { "ProductContent" },
};
var manager = new SearchManager(AppContext.Current.ApplicationName);
var results = manager.Search(criteria);

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.

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 *