Sitecore Commerce - Introduction
Tags: sitecore, sitecorecommerce, dotnet, csharp
Published: 2020-01-21 01:27:35

<b>Introduction</b>
Hi Guys,
This is my first article on DEV. I have planned to write a series of articles with Sitecore Commerce. The idea is to cover from the beginning to reach a point where everyone can understand it and extend the commerce functionalities as required. You do not need to be a Sitecore certified to understand this. What you need is basic understanding of dotnet (C#) knowledge

<b>What is Commerce</b>: Commerce is a very generic term. But the main idea is to allow users to fulfill his journey towards a product purchase. It includes a array of products to be stored somewhere as a part of inventory to shipment. Fulfilling is really important, mainly people are being fulfilled via "delivery" or "pickup". 

A typical fulfillment process is as follows:
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/2fp9oilfjak14q81ib6j.JPG)

<b>Problem of commerce domain</b>: Well, why we need commerce solution? The answer can be found from the below challenges:

> Managing inventory
> Managing Cart 
> Managing User
> Managing Cart state (Every state of your current cart)
> Managing fulfillment
> Integrating with existing system to update product status/state and fulfill
> Continuously running job to manage products' stock
> Extend the commerce functionalities for any new features
> Handling pricing and prepare for any sudden discount/pricing etc

Those are couple of challenges lies on commerce domain. But if we start implementing/solving that, a lot more to come. Sitecore brings all of those problems into a solution and named as "Sitecore Commerce". 

<b>Sitecore Commerce</b>: Sitecore commerce is a dotnetcore based solution which provides solution of those mentioned problem and has a lot of extension points. Sitecore commerce follows odata to provide APIs so that we can integrate it with any platform. Note, You really do not need Sitecore platform to work with Sitecore Commerce. But Sitecore provides a lot of very useful business applications (Written in JSS in 9 onward but in Speak for previous version i.e 8) which you can not use if you do not use Sitecore as a platform. 

<b>Commerce Business Tools (Commerce 8.*)</b>: There are couple of business tools provided by Sitecore out of the box

> Merchandise manager: To manage products catalog
> Customer and Order Manager: Manage the orders
> Pricing and Promotions: Manage pricing and promotion
etc

<b>Sitecore Commerce Architecture</b>: Sitecore commerce uses couple of patterns together to implement the solution and follows very easy and organize approach.  I will brief some of the Sitecore Commerce's terms first before elaborating this.

<b>Pipeline</b>: Pipeline is a thing which can hold a collection of individual process/block and can pass the result to next subsequent processes/blocks. Those who are familiar with Sitecore, might relate this pipeline with Sitecore pipeline but it's slightly different. The ways commerce pipeline works are as follows:

> There is no config files to create/patch pipelines. We need to use code to create or patch a pipeline. I will show an example soon

> Pipelines can hold blocks. A single pipeline might have 10 blocks. When we trigger a pipeline, it basically triggers all the blocks one by one and pass the result as an argument to next block and so on.

> Pipelines and Blocks are the extension point. All the foundation pipelines are already written by Sitecore. We sometimes need to add our own pipelines/blocks or replace the existing ones. 

Example of creating a new pipeline with a single block

```c#
  services.Sitecore().Pipelines(config => config
             .AddPipeline<ISamplePipeline, SampleDiscountPipeline>(
                    configure =>
                        {
                            configure.Add<MemberDiscountBlock>();
                        })
      );
```
  
Another example when a pipeline is already exists, we will add a new block in it:

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

                        })

                        );
```

Same way, we can use configure.Replace<>() to replace a existing block.

<h2>Pipelines and blocks are the building blocks of Sitecore commerce <h2>

<b>Command </b>: Sitecore commerce uses command pattern to trigger any command. An example would be:

You have to execute a product to be added in the cart. For that, there would be some sort of IAddProductToCart command, once that fires up, command executes necessary pipelines and collect the result and return to caller.

<b>Entity </b> The heart of commerce: Entity is nothing but a object represents in JSON. Any changes in the cart or anything like add a product or remove a product, add fulfillment address, method etc will update the entity. In short, by examining an entity, we will know the state of the cart of a user.

<b>Component </b>: Contains the unit piece of business of a certain feature 

<b>Policy</b> : Component can hold policy which will control how a certain component will work.


In my next article, i will briefly explain about Entity, policy and component how all works together to solve a commerce problem. I will try to explain a real world scenario using those. As a intro, i believe it's already enough. Hold tight, will see you soon


Arif