Hide menu Last updated: Oct 12 2015

Episerver Commerce provides two ways of hooking into order events for integration purposes, so you can take actions during an order's events.

IOrderRepositoryCallback

The IOrderRepositoryCallback handles the callback of orders. Those methods are called when order events (OnCreating, OnCreated, OnUpdating, OnUpdated, OnDeleting, OnDeleted) are triggered. Refer to Order Manipulation to learn how to work with orders.

[ServiceConfiguration(typeof(IOrderRepositoryCallback), Lifecycle = ServiceInstanceScope.Singleton)]
public class SiteOrderRepositoryCallback : IOrderRepositoryCallback
{
    private ILogger _logger = LogManager.GetLogger();

    public void OnCreating(Guid customerId, string name)
    {
        _logger.Information(string.Format("Creating order: customer [{0}], name[{1}].", customerId, name));
    }

    public void OnCreated(OrderReference orderReference)
    {
        _logger.Information(string.Format("Created order {0}: orderid [{1}], customer [{2}], name[{3}].", orderReference.OrderType.ToString(), orderReference.OrderGroupId, orderReference.CustomerId, orderReference.Name));
    }
        
    public void OnUpdating(OrderReference orderReference)
    {
        _logger.Information(string.Format("Updating order {0}: orderid [{1}], customer [{2}], name[{3}].", orderReference.OrderType.ToString(), orderReference.OrderGroupId, orderReference.CustomerId, orderReference.Name));
    }

    public void OnUpdated(OrderReference orderReference)
    {
        _logger.Information(string.Format("Updated order {0}: orderid [{1}], customer [{2}], name[{3}].", orderReference.OrderType.ToString(), orderReference.OrderGroupId, orderReference.CustomerId, orderReference.Name));
    }

    public void OnDeleting(OrderReference orderReference)
    {
        _logger.Information(string.Format("Deleting order {0}: orderid [{1}], customer [{2}], name[{3}].", orderReference.OrderType.ToString(), orderReference.OrderGroupId, orderReference.CustomerId, orderReference.Name));
    }

    public void OnDeleted(OrderReference orderReference)
    {
        _logger.Information(string.Format("Deleted order {0}: orderid [{1}], customer [{2}], name[{3}].", orderReference.OrderType.ToString(), orderReference.OrderGroupId, orderReference.CustomerId, orderReference.Name));
    }
}

Order Context events [Legacy]

OrderContext provides two events for integration purposes. These events are synchronous, so it is important for the actions to be quick or done asynchronously, so the event can return while the action does its work.

[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))] 
public class OrderEventInitialization : IConfigurableModule 
{ 
    public void Initialize(InitializationEngine context) 
    { 
        OrderContext.Current.OrderGroupUpdated += Current_OrderGroupUpdated;
        OrderContext.Current.OrderGroupDeleted += Current_OrderGroupUpdated; 
    }

    private void Current_OrderGroupUpdated(object sender, OrderGroupEventArgs e) 
    { 
         Task.Run(()=> DoSomething(sender as OrderGroup, e)); 
    } 

    private void DoSomething(OrderGroup order, OrderGroupEventArgs e)
    { 
    } 
}

Comments