Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Navigation [hide] [expand]
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Pricing events

New in [8.15.0]

Introduction

This document describes the events that are available for usage when pricing 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 pricing event of the CatalogKeyEventBroadcaster class:

public event EventHandler<PriceUpdateEventArgs> PriceUpdated;

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 priceUpdateEventArgs = eventArgs as PriceUpdateEventArgs;
        if (priceUpdateEventArgs != null)
        {
            RemotePriceUpdated(sender, priceUpdateEventArgs);
        }
    }


    private void RemotePriceUpdated(object sender, PriceUpdateEventArgs priceUpdatedEventArgs)
    {
        //Your action when prices 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 prices are saved through the default implementation, the event will automatically be triggered. In a custom implementation of one of these interfaces, the event must be triggered in order for the system to know when there are changes in prices.

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

public virtual void OnPriceUpdated(object source, PriceUpdateEventArgs args)

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

Example

To trigger an event in your pricing system implementation, add this to your void SetCatalogEntryPrices(IEnumerable<CatalogKey> catalogKeys, IEnumerable<IPriceValue> priceValues); method:

_broadcaster.OnPriceUpdated(this, new PriceUpdateEventArgs(catalogKeys.ToList()));

Last updated: Aug 04, 2015