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() + 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)new string[1]  {   inventorySet.Id   }, CommerceEntity.ListName());

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()), 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 list = result.List;
                if (list != null)
                    list.Items.ForEach((Action)(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();
ICache cache = (ICache)null;
List sellableItems = (List)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;
  if (sellableItems != null)
   {
     //custom code to iterate over sellable items
   }                    
 }
 else
 {
  sellableItems = new List();
  FindEntitiesInListArgument entitiesInListArgument = 

 

 	new FindEntitiesInListArgument(typeof(SellableItem), string.Format("{0}", 

 

 	 (object)CommerceEntity.ListName()), 0, int.MaxValue);

 

     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>(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

 

         

  CommerceEntity entity = await currentBlock._findEntityPipeline.Run

 

	(new FindEntityArgument(typeof(SellableItem),

 

 $"{CommerceEntity.IdPrefix()}6003213", false), context);
             var sellableItem = entity as SellableItem
               if (sellableItem == null)
                {
                    await commerceContext.AddMessage(
                        commerceContext.GetPolicy().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();

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

 

					variants.ChildComponents)
                        {
                            var variantData = (ItemVariationComponent)variant;

 

 					// all custiom logic to read some data from variant

                        }                        
                    }                   
                }

 

 

 

 

 

 

SEARCH ARTICLES

CATEGORIES

Sitecore 144
Commerce 100
Web Development 100
Sitecore Commerce 83
Sitecore Experience Commerce 9 64
Sitecore Experience Commerce 59
Content Management 55
eCommerce 48
B2B eCommerce 42
Sitecore Experience Platform 39
Sitecore Platinum Partner 39
Architecture 34
Insite 29
User Experience 26
Strategy 22
B2C eCommerce 21
B2B Commerce Blogs 21
commerceconnect 21
CloudCraze 20
SaaS (Software as a Service) 20
Cloud 17
Commerce Server 17
Salesforce B2B Commerce Cloud 16
Mobile 13
Search 13
Plugins 12
Analytics 12
Application Development 12
Digital Transformation 11
Sitecore Symposium 11
SPEAK 10
Helix 10
DMS 8
Social 8
Business Process 7
Authentication 7
5 Reasons 7
BI and Big Data 6
Data Visualization 6
Sitecore Presentation 6
Coveo 6
NET Development 5
Microsoft Azure 5
Automation 5
Front-End Development 4
SaaS (Software as a Service) 4
Digital Strategy 4
Brightcove 4
Press Release 4
Avalara Tax 3
Sitecore Experience Accelerator (SXA) 3
Sitecore Layouts 3
Video 3
SPE 3
Multi-site 3
Multi-lingual 3
Accessibility 3
Habitat 3
Vault 3
Identity 2
Managed Services 2
CDN 2
SMB 2
Cryptocurrency 2
Sitecore Forms 2
Sitecore Experience Commerce 9 Promotions 2
Uncategorized 2
EXM 2
Conversational Commerce 2
Sitecore SaaS 2
Security 2
Unit Testing 2
Headless Architecture 2
Sitecore Experience Awards 2
Google 1
Content Delivery Network 1
Configure Price Quote 1
CPQ 1
Blockchain 1
Coupons 1
Sitecore Rss 1
Artificial Intelligence 1
Machine Learning 1
Okta 1
RFP Process 1
NoSQL 1
Flex Accelerator for Sitecore 1
Reviews 1
SEO 1
Page Labels 1