Friday, January 4, 2019

Sitecore Commerce 8.2.1 - Create a custom order number and few tips.


We got a requirement to generate a custom order number, Here are a few simple steps for that.

Steps 1  - Define a new environment variable in

\Environments\Shops-1.0.0.json


       

      {
        "$type": "Foundation.Commerce.PaymentsDataTest.Engine.Policies.OrderNumberPolicy, Foundation.Commerce.PaymentsData.Engine",
        "TestStoredProcedureName": "[dbo].[Test_GetNextOrderNumber]",
        "TestOrderNumberPrefix": "W",
        "TestDisableReGenerateOrderNumber": true
      },
       
 



Steps 2  - Define a new policy in the Commerce plugin -

Foundation.Commerce.PaymentsData\Engine\Foundation.Commerce.PaymentsData.Engine\Policies\OrderNumberPolicy.cs


       

    public class OrderNumberPolicy : Policy
    {
        /// 
        /// Stored procedure Inserting OrderNumber
        /// 
        public string TestStoredProcedureName { get; set; }

        /// 
        /// Prefix for Order Number
        /// 
        public string TestOrderNumberPrefix { get; set; }
        /// 
        /// Gets or sets a value indicating whether [disable re generate order number].
        /// 
        /// 
        ///   true if [disable re generate order number]; otherwise, false.
        /// 
        public bool TestDisableReGenerateOrderNumber { get; set; }
    }
       
 

Steps 3 - Extend the feature project - here

Feature.Commerce.Payments\Engine\Feature.Commerce.Payments.Engine\Pipelines\Blocks\GenerateOrderNumberBlock.cs



     

  public override async Task Run(Cart cart, CommercePipelineExecutionContext context)
        {
            

            try
            {
                TestOrderNumberPolicy ObjorderNumberPolicy = context.GetPolicy();

                TestorderNumberComponent.OrderNumber = _customComponent.GetNextOrderNumber(context.CommerceContext, orderNumberPolicy.TestStoredProcedureName, orderNumberPolicy.TestOrderNumberPrefix);
            }
            catch (Exception e)
            {
            }
            return cart;
        }
 


Steps 3 -  Custom table for the order number



     

 USE [Test.Website.CustomDetails]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Test.OrderNumberSequencer](
 [Id] [bigint] IDENTITY(10000000,1) NOT NULL,
 CONSTRAINT [PK_Test_Sequence_OrderNumber] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO


Steps 4 -  Sample store procedure to get the latest/incremental number -



     
USE [Test.Website.CustomDetails]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Test.OrderNumberSequencer](
 [Id] [bigint] IDENTITY(10000000,1) NOT NULL,
 CONSTRAINT [PK_Test_Sequence_OrderNumber] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
G


Steps 5- In case if we want to update the sequence of order number -

     
Steps 1 –  exec [dbo].[Test.OrderNumberSequencer] to check the current number
Step 2 – Run this script to update the sequence 

SET IDENTITY_INSERT dbo.Test.OrderNumberSequencer  ON
GO
-- Insert the record which you want to update with new value in identity column
INSERT INTO Test.OrderNumberSequencerId (Id) VALUES(300000)
GO
--Now set the idenetity_insert OFF to back to prevoius track
SET IDENTITY_INSERT Test.OrderNumberSequencerId OFF

Steps 3 Run this sp to see the current number - exec [dbo].[Test.OrderNumberSequencer] - Please take a note of that number


These are a very simple steps if you want to add a custom order number or any values/reference field in the Sitecore commerce engine,

Happy coding :)

No comments:

Post a Comment