DEV Community

Arif
Arif

Posted on

Sitecore Commerce - Pipeline and Block

Introduction

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.

Difference between Sitecore and Sitecore Commerce

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.

Seriously, there is no config? then how to patch (i.e replace, after, before)

Let me give you couple of examples:

Example 1, Adding a new pipeline with a single block

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

Example 2

We are adding a block to a existing pipeline (ICalculateCartPipeline)

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

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

Example 3

We are now replacing a block with our own block:

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

Example 4

We are now adding our own block a block after/before a certain block:

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

Alt Text

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

How Sitecore Commerce Solution uses pipelines

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. (A pipeline can hold not only blocks but also pipelines )

Conclusion

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.

Top comments (0)