HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Order events

Describes order events methods so you can build functionality to take action during order processing in Optimizely Commerce Connect 13.

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)
          { 
          } 
      }