Sitecore Commerce - Pipeline and Block
Arif Uzzaman
Ex-Sitecorean | Ex Solution Architect at Sitecore | XM Cloud | OrderCloud | ContentHub | Solution Architect | Lead Developer | Tech youtuber
Hi,
Those who already worked on Sitecore platform, probably have idea about Pipeline and Processor. The concept is almost same for the pipeline in Sitecore commerce but was slightly modified. In Sitecore commerce, there is no processor. Instead they introduce Block.
A Commerce pipeline can contain multiple blocks (Likewise a Sitecore Pipeline can contain multiple processors) and also other pipeline.
In sitecore, we can add our custom processor and patch that in a specific position of a pipeline. In Commerce, there is no config patching facilities. Instead we can create our custom plugin and can patch our Blocks through the code. There are couple of ways we follow to create a pipeline and block in Sitecore Commerce as follows:
- Option 1
services.Sitecore().Pipelines(config => config
.AddPipeline<IMemberDiscountPipeline, MemberDiscountPipeline>(
configure =>
{
configure.Add<MemberDiscountBlock>();
})
);
We are creating a pipeline called MemberDiscountPipeline which has only one Block.
2. Option 2
services.Sitecore().Pipelines(config => config
.ConfigurePipeline<IAddCartLinePipeline>(
configure =>
{
configure.Add<SpecialDiscountBlock>().Before<ICalculateCartPipelineBlock>();
})
);
Here, we are not creating any new pipeline but adding a block (like process in sitecore) to a existing pipeline called AddCartLinePipeline. We are also telling that our block will execute before "CalculateCartPipelineBlock
3. Option 3
services.Sitecore().Pipelines(config => config
.ConfigurePipeline<IMemberDiscountPipeline>(
configure =>
{
configure.Replace<MemberDiscountBlock, NewMemberDiscountBlock>();
})
);
We are replacing a existing block with the new block.
Block
Block contains a unit peace of work that will modify/add business logic (by adding or modifying Component/Policy or other properties of the entity) to the existing entity (i.e Cart or CartLine) or to a new entity. A typical block signature is as follows:
[PipelineDisplayName("Carts.SpecialDiscountBlock")]
public class SpecialDiscountBlock : PipelineBlock<Cart, Cart, CommercePipelineExecutionContext>
{
/// <summary>
/// The execute.
/// </summary>
/// <param name="arg">
/// The SampleArgument argument.
/// </param>
/// <param name="context">
/// The context.
/// </param>
/// <returns>
/// The <see cref="SampleEntity"/>.
/// </returns>
public override async Task<Cart> Run(Cart cart, CommercePipelineExecutionContext context)
{
Condition.Requires(cart).IsNotNull("The argument can not be null");
cart.Adjustments.Add(new Sitecore.Commerce.Plugin.Pricing.AwardedAdjustment() { Adjustment = new Money(-10), DisplayName = "Global Adjustment for Christmas", Name = "Global Adjustment" });
return await Task.FromResult( cart);
}
}
Likewise Sitecore Processor, Block has "Run" method instead of "Process". I am not going to describe details about this. But a Pipeline executes it's block. A Command executes a Pipeline. And a Api Endpoint (like a get/post of Controller) executes a Command. The sequence is as follows:
End Point > Commands > Pipeline > Block
If we are going to intercept a existing pipeline, then we can patch our block to that pipeline without having a command or endpoint or even a new pipeline.
Next article will describe details about a custom plugin and basic concepts/architecture of Sitecore Commerce and how it works through different micro service layers.
Senior Software Engineer | Enabling digital teams with Dataweavers WebOps? Platform | Sitecore | CX | Digital Transformation | MarTech | Sitecore Upgrade | Sitecore 9, Sitecore 10, Sitecore XM Cloud Certified| Optimizely
6 年Nice but it has been changed a lot in sitecore 9 sxa. This module can give you a lot of surprises. Happy coding and finding.