Sitecore Commerce - Get non commerce Sitecore data - ExternalSettingsComponent

Getting product data

One sellable entity can be in multiple categories which means it has multiple sitecore ids. Every that Sitecore item has its own different Sitecore data like title, short description etc. The first step is to get the right sitecore Id. The entity has the property SitecoreId, but that isn't the Id we need. We need a DeterministicId which we will get by using Sitecore.Commerce.Core.GuidUtility.GetDeterministicGuid by passing entity property SitecoreId and categoryId.

var deterministicId = GuidUtility.GetDeterministicGuid($"{sellableItem.SitecoreId}|{CategoryId.ToLower()}");

Sellable entities have a component ExternalSettingsComponent which stores Sitecore data by sitecoreIds and by languages. I hardcoded to get data in the English language, but if you have multiple languages you can get the current language from context. This block of code returns a dictionary where key is the name of a custom Sitecore field and value is the value of that field. Also we implemented it as a pipeline so it can be accessed anywhere.

var result = new Dictionary<string, string>();
var externalSettingsComponent = sellableItem.GetComponent<ExternalSettingsComponent>();
var settingsCollection = JsonConvert.DeserializeObject<Dictionary<Guid, Dictionary<string, Dictionary<string, string>>>>(externalSettingsComponent.Settings);
if (!settingsCollection.ContainsKey(deterministicId)) return result;
var externalSettings = settingsCollection[deterministicId];
var languageSettings = externalSettings["en"];
return languageSettings;

Getting non product data

For getting Sitecore data that are in Content/NameOfTheProject part of Sitecore tree we will use out of a box pipeline Sitecore.Commerce.Plugin.Managment.IGetItemByIdPipeline. The pipeline also returns a dictionary where key is the name of a custom Sitecore field and value is the value of that field.

var argument = new ItemModelArgument(sitecoreId);
var sitecoreItem = await _commander.Pipeline<IGetItemByIdPipeline>().Run(argument, context);
if (sitecoreItem != null && sitecoreItem.ContainsKey("Title")) 
title = sitecoreItem["Title"].ToString();

This has been implemented and tested on Sitecore Experience Commerce 9.2

Thank You for Reading
Share This Page