allBlogsList

Commonly Used Sitecore Commerce Pipelines

**While writing new Sitecore Commerce plugins, I noticed that there a few pipelines that were required in almost all blocks. So i thought of putting together a list of pipelines so that it can be useful for someone who is new to Sitecore Commerce Plugins.

IDoesEntityExistPipeline** - Useful in checking if an entity of a type and an identifier exists in database or not. It can also create one if you set the parameter to shouldCreate=true

Input - FindEntityTypeArgument - type of entity, identifier and shouldCreate (true/false)

Returns - Bool

For example: To find if a particular inventory set exists or not :

// arg.Name = habitat_inventory or name of your inventory set.
string id = CommerceEntity.IdPrefix<InventorySet>() + arg.Name;
var doesExist = await currentBlock._doesEntityExistPipeline.Run(new FindEntityArgument(typeof(InventorySet), id, false), context);

IPersistEntityPipeline - If you are creating an entity, then to persist it to database, you will need to use this pipeline.

Input - PersistEntityArgument - The entity to be persisted and the context.

Returns - PersistEntityArgument

InventorySet inventorySet = new InventorySet();
//set values for inventorySet 
PersistEntityArgument persistEntityArgument = await currentBlock._persistEntityPipeline.Run(new PersistEntityArgument(inventorySet), context);

IAddListEntitiesPipeline -  Creates an entry in the table  [SitecoreCommerce9_SharedEnvironments].[dbo].[CommerceLists] with the given CommerceEntityId . Maintains relationship with the commerce entity and lists

For example, the following command, creates an entry in the table CommerceLists that looks like this

ListEntitiesArgument entitiesArgument1 = new ListEntitiesArgument((IEnumerable<string>)new string[1]  {   inventorySet.Id   }, CommerceEntity.ListName<InventorySet>());
ListEntitiesArgument entitiesArgument2 = await inventorySetBlock._addListEntitiesPipeline.Run(entitiesArgument1, context);

SQL RESULT

We can then use the FindEntityInList pipeline to retrieve this entry.

IFindEntitiesInListPipeline - Returns a list of Commerce Entity matching the conditions in argument.

Input - FindEntitiesInListArgument - type of entity to look for, listname, skip count and take count

Output - FindEntitiesInListArgument  - List of Commerce Entities if found matching the criteria in input.

Example:

FindEntitiesInListArgument result= await currentBlock._findEntitiesInListPipeline.Run(new FindEntitiesInListArgument(typeof(InventorySet), string.Format("{0}", (object)CommerceEntity.ListName<InventorySet>()), 0, int.MaxValue), context);

The above statement will return a list of all Inventory sets from the table [SitecoreCommerce9_SharedEnvironments].[dbo].[CommerceLists]. You can then iterate and cast the item you want to InventorySet and get data.

if (result!= null)
            {

                CommerceList<CommerceEntity> list = result.List;

                if (list != null)

                    list.Items.ForEach((Action<CommerceEntity>)(item =>

                    {

                        InventorySet inventorySet = (InventorySet)item;

                        //just an example but you can add any custom logic.

  inventorySets.Add(inventorySet);

                    }));

            }

IGetEnvironmentCachePipeline - Get an item that is in the cache.

Input - EnvironmentCacheArgument - Cache Name

Output : Cache

string cacheKey = string.Format("{0}|{1}|{2}", (object)context.CommerceContext.Environment.Name, (object)context.CommerceContext.CurrentLanguage(), (object)(context.CommerceContext.CurrentShopName() ?? ""));
ProductCachePolicy cachePolicy = context.GetPolicy<ProductCachePolicy>();
ICache cache = (ICache)null;
List<SellableItem> sellableItems = (List<SellableItem>)null;
if (cachePolicy.AllowCaching)
 {
  IGetEnvironmentCachePipeline cachePipeline = currentBlock._cachePipeline;
  EnvironmentCacheArgument environmentCacheArgument = new EnvironmentCacheArgument();
  environmentCacheArgument.CacheName = cachePolicy.CatalogsCacheName
  cache = await cachePipeline.Run(environmentCacheArgument, context);
  sellableItems = await cache.Get(cacheKey) as List<SellableItem>;
  if (sellableItems != null)
   {
     //custom code to iterate over sellable items
   }                    
 }
 else
 {
  sellableItems = new List<SellableItem>();
  FindEntitiesInListArgument entitiesInListArgument = 
     foreach (CommerceEntity commerceEntity in 
	(await currentBlock._findEntitiesInListPipeline.Run(entitiesInListArgument,
	 (IPipelineExecutionContextOptions)context.ContextOptions)).List.Items)
     {
       //custom code to iterate over sellable items
     }
     if (cachePolicy.AllowCaching)
     {
       if (cache != null)
          await cache.Set(cacheKey,
	 (ICachable)new Cachable<List<SellableItem>>(sellableItems, 1L), 
	cachePolicy.GetCacheEntryOptions());
     }
 

Last but not the least, IFindEntityPipeline - Finds an entity of a given type and id. If not found, it can also create an entity without persisting it to the database.

Input - FindEntityArgument - type of entity to find, identifier and shouldCreate if not found

Output - CommerceEntity

CommerceEntity.IdPrefix

Example

To find a sellable item with and Id = 6003213

	(new FindEntityArgument(typeof(SellableItem),
 $"{CommerceEntity.IdPrefix<SellableItem>()}6003213", false), context);
             var sellableItem = entity as SellableItem
               if (sellableItem == null)
                {
                    await commerceContext.AddMessage(
                        commerceContext.GetPolicy<KnownResultCodes>().Error,
                        "EntityNotFound",
                        new object[] { "6002321" },
                        $"Entity  was not found.");
                    return false;
                }
               //if sellableItem not null then do;
               if ((entity is SellableItem))
                {
                    SellableItem sellableItem = entity as SellableItem;
                    var variants = sellableItem.GetComponent<ItemVariationsComponent>();

                    if (variants != null && variants.ChildComponents.Count > 0)
                    {
                        foreach (ItemVariationComponent variant in 
					variants.ChildComponents)
                        {
                            var variantData = (ItemVariationComponent)variant;

                        }                        
                    }                   
                }