Creating a Custom Customer Number Plugin - Sitecore Commerce Engine
Sitecore Commerce Engine out of the box uses guid to represent customer numbers when a user signs up.
You may have a client who is migrating to Sitecore Commerce and want to preserve the way their customer number is formatted in their legacy system.
Step 1
Under the Commerce Engine Solution for your site, create a new .NET Core project and name it as you wish. I named mine Sitecore.Commerce.Plugin.CustomizeCustomerNumber .
I am using Visual Studio 2017 and my .Net framework is 4.6.2
Step 2
Add Sitecore Nugget
Sitecore.Commerce.Core 2.2.29
Sitecore.Commerce.Plugin.Catalog 2.2.46
Sitecore.Commerce.Plugin.Customers 2.2.10
Step 3
Add a folder name Pipelines and under that folder, add a Blocks folder
Under the blocks folder, add a public class for customizing customer number and name it as you wish. I named mine CustomizeCustomerNumberBlock I also followed the naming convention and ended the name with Block.
Inherit from PipelineBlock
Implement the required Run method with its parameters.
In the method, you need to replace the AccountNumber, Id and FriendlyId of the passed Customer parameter with the desired custom value.
This can be done either by calling and external system of referring to a config. In my case, I have opted for sequential customer numbers so, I simply increment a new customer numbers count by one for a new customer.
public class CustomizeCustomerNumberBlock : PipelineBlock
{
///
///
///
private readonly FindEntitiesInListCommand _findEntitiesInListCommand;
///
///
///
///
public CustomizeCustomerNumberBlock(FindEntitiesInListCommand findEntitiesInListCommand)
{
_findEntitiesInListCommand = findEntitiesInListCommand;
}
///
///
///
///
///
///
public override Task Run(Customer customer, CommercePipelineExecutionContext context)
{
// Ensure customer is not null and has a name
Condition.Requires(customer).IsNotNull( "The customer can not be null");
Condition.Requires(customer.UserName).IsNotNullOrEmpty( "The customer user name can not be null");
// This is where we proseed to generating the new custom customer number either directly or by calling an external service.
customer.AccountNumber = GenerateNewCustomerNumber(context, _findEntitiesInListCommand);
customer.Id = $"{(object) CommerceEntity.IdPrefix()}{(object) customer.AccountNumber}";
customer.FriendlyId = customer.AccountNumber;
return Task.FromResult(customer);
}
///
/// /
///
///
///
///
private static string GenerateNewCustomerNumber(CommercePipelineExecutionContext context, FindEntitiesInListCommand findEntitiesInListCommand)
{
// get all existing customers.
var customers = (IEnumerable)findEntitiesInListCommand.Process(context.CommerceContext, CommerceEntity.ListName(), 0, int.MaxValue).Result.Items;
// Total existing customers
var customerCount = customers.Count();
if (!customers.Any()) return "customer1";
// use the info you have to generate an appropriate customer 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 customers incremented by 1
// Return customer count and increment by 1 as the new customer number.
var nextOrderNumber = customerCount + 1;
return "customer" + nextOrderNumber.ToString();
}
}
Step 4
Create a configuration file at the root of your project and name it ConfigureSitecore inherit from IConfigureSitecore and implement the interface.
In the implemented method, add the line below into the so that the CustomizeCustomerNumberBlock code block is implemented just before Sitecore Commerce runs the CreateCustomerBlock
.ConfigurePipeline
public class ConfigureSitecore : IConfigureSitecore
{
///
/// The configure services.
///
///
/// The services.
///
public void ConfigureServices(IServiceCollection services)
{
var assembly = Assembly.GetExecutingAssembly();
services.RegisterAllPipelineBlocks(assembly);
services.Sitecore().Pipelines(config => config
.ConfigurePipeline(c => {c.Add().Before();})
);
services.RegisterAllCommands(assembly);
}
}
The plugin is now ready to put to a test.
Add it to your solution and try it out.
You may down load the sample plugin from Github here: Github Link