November Happy Hour will be moved to Thursday December 5th.

Loading...
Applies to versions: 10-13
Other versions:
ARCHIVED This content is retired and no longer maintained. See the version selector for other versions of this topic.

Changing order number sequence [Legacy]

Recommended reading 

When a customer submits a purchase in Episerver Commerce, the cart is converted to a PurchaseOrder by calling the Cart.SaveAsPurchaseOrder method. When this method is called, an order number (PurchaseOrder.TrackingNumber property) is assigned to the PurchaseOrder using a default algorithm. You can override the algorithm to set an order number that is more appropriate to your implementation via the TrackingNumber property.

Classes in this topic are available in the Mediachase.Commerce.Orders namespace.

How it works

The Cart has a public delegate property called CreateOrderNumber.

Example: the delegate signature for CreateOrderNumber

public delegate string CreateOrderNumber(Cart cart);

This delegate is null by default. When you execute the SaveAsPurchaseOrder method with no set delegate, the default delegate is created and executed. However, if you set the delegate with a custom method prior to executing the method, the custom delegate is run.

Example: the part of the SaveAsPurchaseOrder method that runs the custom delegate

// from inside SaveAsPurchaseOrder method
// Set tracking number
if (this.OrderNumberMethod == null)
{
    this.OrderNumberMethod = new CreateOrderNumber(GenerateOrderNumber);
}

purchaseOrder.TrackingNumber = OrderNumberMethod(this);

Example: implementing a custom order number generator

//custom order number method
private string CustomOrderNumber(Cart cart)
{
    //implement custom logic here to return a custom order number in string format
}

...
//somewhere in your method where you are completing the checkout process
//Set the Cart order generator delegate
CartHelper.Cart.OrderNumberMethod = new CreateOrderNumber(CustomOrderNumber);

//now convert the cart into a purchase order
CartHelper.Cart.SaveAsPurchaseOrder();
Do you find this information helpful? Please log in to provide feedback.

Last updated: Apr 01, 2021

Recommended reading