Sitecore Commerce - Pipeline and Block
Tags: sitecore, block, sitecorecommerce, pipeline
Published: 2020-01-25 03:51:41

<h4>Introduction </h4> Pipelines are the extension point of Sitecore and Sitecore Commerce. Although there is a difference in terms of how we implement from Sitecore to Sitecore Commerce. 

<h4>Difference between Sitecore and Sitecore Commerce </h4>
In Sitecore, we register pipelines in Configuration files. We then register processors in those pipelines. There is no configuration approach in Commerce for pipelines. We need to register pipelines in code and then we can attach blocks in those pipelines. In Sitecore XM/XP, each of the processors has a PipelineArgs from where we can pass our data to the next processor whereas, in Commerce, the returned result of the first block will be used as an input to the next Block and so on. 

<h4>Seriously, there is no config? then how to patch (i.e replace, after, before) </h4>
Let me give you couple of examples:



<h3>Example 1, Adding a new pipeline with a single block </h3>

```c#
public void ConfigureServices(IServiceCollection services)
        {
            var assembly = Assembly.GetExecutingAssembly();
            services.RegisterAllPipelineBlocks(assembly);
     services.Sitecore().Pipelines(config => config
             .AddPipeline<IMemberDiscountPipeline, MemberDiscountPipeline>(
                    configure =>
                        {
                            configure.Add<MemberDiscountBlock>();
                        })
      );
 }

```

<h3>Example 2 </h3> We are adding a block to a existing pipeline (ICalculateCartPipeline)

```c#

      public void ConfigureServices(IServiceCollection services)
        {
            var assembly = Assembly.GetExecutingAssembly();
            services.RegisterAllPipelineBlocks(assembly);

            services.Sitecore().Pipelines(config => config
             .ConfigurePipeline<ICalculateCartPipeline>(
                    configure =>
                    {
                        configure.Add<CalculateDiscountBlock>();
                    })
                        );
        }

```
<h3>Example 3 </h3> We are now replacing a block with our own block:

```c#

services.Sitecore().Pipelines(config => config
             .ConfigurePipeline<IMemberDiscountPipeline>(
                    configure =>
                    {
                        configure.Replace<MemberDiscountBlock, NewMemberDiscountBlock>();
                    })
      );

```
<h3>Example 4 </h3> We are now adding our own block a block after/before a certain block:

```c#

services.Sitecore().Pipelines(config => config
             .ConfigurePipeline<IAddCartLinePipeline>(
                    configure =>
                        {
                            configure.Add<SpecialDiscountBlock>().Before<ICalculateCartPipelineBlock>();
                        })
                        );

```

Pretty easy and cool, right? It's really important to know how many pipelines and blocks are there in the Commerce System and how they work together. In Sitecore, we have "showconfig.aspx" where we can see all of them together. But in Commerce, there is no such thing but a log where we can see. It's called node configuration. If we explore the commerce hosting directory, we will have a log in the following path: "wwwroot/log/NodeConfiguration_Deployment.....log". If we explore this file, we will see something like this:


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/n66hu4iuww6ucqj90xkq.PNG)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/m52iffdsb291ybpurk2s.PNG)

This file should contain all of the registered (including your's one) with all the blocks associated with the pipelines.

<h4>How Sitecore Commerce Solution uses pipelines </h4>
Sitecore commerce already provides core pipelines and blocks. We just need to use them and as necessary we might need to add blocks to them and sometimes obviously we will need to create our own pipeline and we can push that to an existing pipeline. (<b>A pipeline can hold not only blocks but also pipelines </b>)

<h4>Conclusion </h4>
I hope now it's clear what is Commerce Pipeline and Block. I will use them to create our own feature in Commerce in my later articles. I will publish an article about the implementation of Pipeline and Block to solve a simple business problem in my next article.