Change Order Number generator

By default, the orders numbers are generated by an instance of EPiServer.Commerce.Order.IOrderNumberGenerator.

The default number generation is:

 public string GenerateOrderNumber(IOrderGroup orderGroup)
         int num = new Random().Next(100, 999);
         return $"PO{orderGroup.OrderLink.OrderGroupId}{num}";

So, to change this, first we need to inject a custom implementation and remove the default one. On an initialization module that depends on EPiServer.Web.InitializationModule have the following:

(IServiceConfigurationProvider)services.AddSingleton<IOrderNumberGenerator, MyCustomOrderNumberGenerator>();

And my custom order generator, can be like this:

 public class MyCustomOrderNumberGenerator: IOrderNumberGenerator
        private readonly string prefix;
        private readonly string version;

        public MyCustomOrderNumberGenerator()
            this.prefix = ConfigurationManager.AppSettings["OrderNumberGenerator:prefix"] ?? "PO";
            this.version = ConfigurationManager.AppSettings["OrderNumberGenerator:version"] ?? "00";

        public string GenerateOrderNumber(IOrderGroup orderGroup)
            return $"{prefix}{version}{new Random().Next(100, 999)} 

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: