Loading...
Area: Episerver Commerce
Applies to versions: 12.12 and higher
Other versions:

Order events

Recommendations [hide]

Order events in Episerver Commerce provides methods for hooking into order events for integration purposes, so you can build functionality to take action during for example order processing.

IOrderEvents

The IOrderEvents available in the EPiServer.Commerce.Order namespace, handles events for orders processed via IOrderRepository.

IOrderEvents contains the order events SavingOrder, SavedOrder, DeletingOrder, and DeletedOrder, triggered when manipulating an order. See Order Manipulation.

public class OrderEventListener
  {
    private readonly IOrderRepository _orderRepository;
    private readonly IOrderEvents _orderEvents;
    private readonly ILogger _logger = LogManager.GetLogger(typeof(OrderEventListener));

    public OrderEventListener(IOrderRepository orderRepository, IOrderEvents orderEvents)
      {
        _orderRepository = orderRepository;
        _orderEvents = orderEvents;
      }

    public void AddEvents()
      {
        _orderEvents.SavedOrder += OrderEventsOnSavedOrder;
        _orderEvents.DeletingOrder += OrderEventsOnDeletingOrder;
      }

    private void OrderEventsOnSavedOrder(object sender, OrderEventArgs orderEventArgs)
      {
        var po = orderEventArgs.OrderGroup as IPurchaseOrder;
        if (po != null)
          {
            _logger.Information($"Order {po.OrderNumber} was saved");
          }
      }

    private void OrderEventsOnDeletingOrder(object sender, OrderEventArgs orderEventArgs)
      {
        var po = _orderRepository.Load<IPurchaseOrder>(orderEventArgs.OrderLink.OrderGroupId);
        _logger.Information($"Order {po.OrderNumber} is being deleted.");
      }

    public void RemoveEvents()
      {
        _orderEvents.DeletingOrder -= OrderEventsOnDeletingOrder;
        _orderEvents.SavedOrder -= OrderEventsOnSavedOrder;
      }
  }

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. In this way, 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)
      { 
      } 
  }

Related topics

Do you find this information helpful? Please log in to provide feedback.

Last updated: Oct 24, 2016

Recommendations [hide]