|
| 1 | +--- |
| 2 | +description: Learn how to customize the default order number generated in Umbraco Commerce. |
| 3 | +--- |
| 4 | + |
| 5 | +# Order Number Customization |
| 6 | + |
| 7 | +In Umbraco Commerce, the default order number generation can be customized by implementing the `IOrderNumberGenerator` interface. This interface defines two methods: `GenerateCartNumber(Guid storeId)` and `GenerateOrderNumber(Guid storeId)`, which you can override to create a custom numbering system. |
| 8 | + |
| 9 | +## Implementing a Custom Order Number Generator |
| 10 | + |
| 11 | +To create a custom order number generator, define a class that implements the `IOrderNumberGenerator` interface, for example, `CustomOrderNumberGenerator.cs`: |
| 12 | + |
| 13 | +{% code title="CustomOrderNumberGenerator.cs" %} |
| 14 | + |
| 15 | +```csharp |
| 16 | +using Umbraco.Commerce.Core.Generators; |
| 17 | + |
| 18 | +public class CustomOrderNumberGenerator : IOrderNumberGenerator |
| 19 | +{ |
| 20 | + public string GenerateCartNumber(Guid storeId) |
| 21 | + { |
| 22 | + // Implement custom logic for cart numbers |
| 23 | + } |
| 24 | + |
| 25 | + public string GenerateOrderNumber(Guid storeId) |
| 26 | + { |
| 27 | + // Implement custom logic for order numbers |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +{% endcode %} |
| 33 | + |
| 34 | +## Registering the Custom Implementation |
| 35 | + |
| 36 | +After creating your custom generator, register it in `Program.cs` to replace the default implementation: |
| 37 | + |
| 38 | +{% code title="Program.cs" %} |
| 39 | + |
| 40 | +```csharp |
| 41 | +builder.Services.AddUnique<IOrderNumberGenerator, MyOrderNumberGenerator>(); |
| 42 | +``` |
| 43 | + |
| 44 | +{% endcode %} |
| 45 | + |
| 46 | +The `AddUnique` method ensures that your custom generator replaces the default `IOrderNumberGenerator`. For more details on dependency injection, see the [Dependency Injection](dependency-injection.md) article. |
| 47 | + |
| 48 | +## Important Considerations |
| 49 | + |
| 50 | +Before implementing a custom order number generator, be aware of the following: |
| 51 | + |
| 52 | +- **Performance Implications:** Sequential order numbers may require database access to ensure uniqueness, which can become a performance bottleneck under heavy load. The default Umbraco Commerce generator uses a timestamp and random seed based algorithm to create numbers in memory, avoiding database hits. |
| 53 | +- **Order Number Gaps:** In Umbraco Commerce, order numbers are generated before redirecting to the payment gateway. If a customer cancels or modifies their order after an order number has been assigned, a new number is generated for the subsequent attempt, leading to gaps in the sequence. This behavior can be problematic if sequential numbering is used for official records like VAT receipts, as such records typically require continuous sequences without gaps. |
| 54 | +- **Accounting Considerations:** Umbraco Commerce is not designed as an accounting platform. If strict sequential numbering is required for accounting purposes, it is recommended to integrate with a dedicated accounting system to handle such requirements. |
| 55 | + |
| 56 | +By understanding these factors, you can implement a custom order number generator that aligns with your specific requirements while maintaining optimal performance and compliance. |
0 commit comments