• Twitter
  • LinkedIn

Customizing Sitecore Commerce Engine Order Number

Create a new project of type .Netcore Class library in your Sitecore Commerce Engine solution

I will name the one used in this blog "Sitecore.Plugin.CustomOrderNumber"

Open the project.json file and replace its content with the following

 
{
  "version": "1.0.1",


  "dependencies": {
    "Sitecore.Commerce.Core": "1.2.*",
    "Sitecore.Commerce.Plugin.CsAgent": "1.2.159",
    "Sitecore.Commerce.Plugin.Fulfillment": "1.2.159",
    "Sitecore.Commerce.Plugin.Management": "1.2.159",
    "Sitecore.Commerce.Plugin.Catalog": "1.2.159",
    "Sitecore.Commerce.Plugin.Catalog.Cs": "1.2.159"
  },


  "frameworks": {
    "net461": {
    }
  }
}

Create a folder Pipelines and inside that create a subfolder Blocks:

In the blocks folder, create a new class and name it as you like. I will name the one in this blog

"CustomOrderNumber.cs"

Get the class to inherit from PipelineBlock

Implement the abstract class.

Copy the code below into the class, it is well commented for clarity

 
   public class CustomOrderNumber : PipelineBlock
    {
        /// 
        /// 
        /// 
        private readonly FindEntitiesInListCommand _findEntitiesInListCommand;

        /// 
        /// 
        /// 
        /// 
        public CustomOrderNumber(FindEntitiesInListCommand findEntitiesInListCommand) : base((string)null)
        {
            // We need to use this to get all existing orders placed before the new order
            _findEntitiesInListCommand = findEntitiesInListCommand;
        }

        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public override Task Run(Order order, CommercePipelineExecutionContext context)
        {
            // We need to ensure the order object is not null.
            Condition.Requires(order).IsNotNull("The order can not be null");

            // We call a methos that will generate our new order number. We pass parameters that may help in formulating the order number.
            order.OrderConfirmationId = GetCustomOrderNumber(order, context, _findEntitiesInListCommand);

            // return the new order so that it can be sent to the entities databse
            return Task.FromResult(order);
        }

        /// 
        /// You may customized what is returned here based on number od existing orders or date or get number from external system
        /// 
        /// 
        /// 
        /// 
        /// 
        private string GetCustomOrderNumber(Order order, CommercePipelineExecutionContext context, FindEntitiesInListCommand findEntitiesInListCommand)
        {
            //Get Contact Component which contains customer information
            var contactComponent = order.GetComponent();

            // get all existing orders.
            var orders = (IEnumerable)findEntitiesInListCommand.Process(context.CommerceContext, CommerceEntity.ListName(), 0, int.MaxValue).Result.Items;

            // Total orders
            var orderCount = orders.Count();

            // use the info you have to generate an appropriate order number. You may also use the data you have to call an external system.
            // in this instance we will just return the number of existing orders incremented by 1
            // Return order count and increment by 1 as the new order number.
            if (orders.Any())
            {
                var nextOrderNumber = orderCount + 1;
                return nextOrderNumber.ToString();
            }

            // return a random guid if ther was an isssue retriving existing orders or all else failed.
            return Guid.NewGuid().ToString("B");
        }


    }


To add the reference of the new plugin to your Commerce Engine solution,

Goto your base Solution plugin such as Sitecore.Commerce.Plugin.AdventureWorks, open its project.json file and add reference to the new plugin as shown below.

Also open the class ServiceCollectionExtensions.cs and add the pipeline call below towards the end of ConfigureCartPipelines method:

.ConfigurePipeline(builder => builder.Replace())

You may down load the sample plugin from Github here:

[Download](https://github.com/XCentium/SC-Plugin-OrderNumber "Download plugin")

Related Blogs

Latest Blogs