This content is archived. See latest version here

Last updated: Aug 04 2015

Inventory events

New in [8.15.0]

This document describes the events that are used when inventory information is updated in Commerce. The events are handled through the CatalogKeyEventBroadcaster class. 

Listening to events

To listen to new events, register your method to the event of the CatalogKeyEventBroadcaster class:

public event EventHandler<InventoryUpdateEventArgs> InventoryUpdateEvent;

To listen to remote events, first you need to get the event from Events engine

    public void AddEvent()
    {
        Event ev = Event.Get(CatalogKeyEventBroadcaster.CatalogKeyEventGuid);
        ev.Raised += CatalogKeyEventUpdated;
    }

    private void CatalogKeyEventUpdated(object sender, EventNotificationEventArgs e)
    {
        var eventArgs = (CatalogKeyEventArgs)DeSerialize((byte[])e.Param);
        var inventoryUpdatedEventArgs = eventArgs as InventoryUpdateEventArgs;
        if (inventoryUpdatedEventArgs != null)
        {
            RemoteInventoryUpdated(sender, inventoryUpdatedEventArgs);
        }
    }

    private void RemoteInventoryUpdated(object sender, InventoryUpdateEventArgs inventoryUpdatedEventArgs)
    {
        //Your action when inventories are updated remotely.
    }

    protected virtual CatalogKeyEventArgs DeSerialize(byte[] buffer)
    {
         var formatter = new BinaryFormatter();
         var stream = new MemoryStream(buffer);
         return formatter.Deserialize(stream) as CatalogKeyEventArgs;
    }

Ensure that you call your AddEvent in an IInitializationModule.

Triggering events

When inventories are saved through the default implementation, the event will automatically be triggered. In a custom implementation of the interface, the event must be triggered in order for the system to know when there are changes in inventories.

To broadcast the events, use the CatalogKeyEventBroadcaster class. This class has one public method for triggering inventory events:

public virtual void InventoryUpdated(object source, InventoryUpdateEventArgs args)

Whenever changes are done to the inventory system, you can call the method to raise an event.

Example

To trigger an event in your inventory system implementation, add this to your void Save(IEnumerable<InventoryRecord> records); method:

_broadcaster.InventoryUpdated(this, new InventoryUpdateEventArgs(catalogKeys.ToList()));

Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/