Quan Mai
Jul 23, 2015
  4374
(10 votes)

New events for price and inventory updates

We hear you! Customers of Commerce have been requesting the ability to fire and/or listen to events when there are changes in prices and inventories. In the upcoming Commerce - version 8.15, we will provide new APIs to let you do that with ease.

Important note: The API:s have been slightly revised in 8.16, so this post was updated to reflect the changes, as we highly recommed you to upgrade to latest Commerce version. 

Listening to new events from price and inventory system

you'll need to implement a class two methods, which take the relevant arguments:

        public void PriceUpdated(object sender, PriceUpdateEventArgs args)

        {

        //Your action when the prices are updated

        }

        public void InventoryUpdated(object sender, InventoryUpdateEventArgs args)

        {

        //Your action when the inventories records are updated.

        }



The sender in case of PriceUpdated is a IEnumerable<IPriceValue>, while
in InventoryUpdated is a IEnumerable<InventoryRecord>

To process remote events, you will need to get the event from EPiServer Framework event, like this:

    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 priceUpdatedEventArgs = eventArgs as PriceUpdateEventArgs;

        if (priceUpdatedEventArgs != null)

        {

            RemotePriceUpdated(sender, priceUpdatedEventArgs);

            return;

        }

        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.

    }

 

    private void RemotePriceUpdated(object sender, PriceUpdateEventArgs priceUpdatedEventArgs)

    {

        //Your action when prices are updated remotely.

    }



You can only get the affected CatalogKey list from remote events, so it can be used to validate cache and so on.

And then register that new class into system. This should be done in an IInitializationModule, so you can start listening to the events when your site start:

In public void Initialize(InitializationEngine context) method:

        var broadcaster = context.Locate.Advanced.GetInstance<CatalogKeyEventBroadcaster>();

        var listener = context.Locate.Advanced.GetInstance<CatalogKeyEventListener>();

        listener.AddEvent();

        broadcaster.InventoryUpdated += listener.InventoryUpdated;

        broadcaster.PriceUpdated += listener.PriceUpdated;



Where CatalogKeyEventListener is your class which implements those methods above.

Broadcasting new events:

When prices or inventories are saved through our default implementation, those events will be fired automatically. If you have custom implementation of one of those interfaces, and you wish to let other parts of system know when there are changes in prices and inventories, you will need to fire your event in your implementation.

To broadcast the events, use CatalogKeyEventBroadcaster class. This class has two public methods for firing events:

   public virtual void OnPriceUpdated(object source, PriceUpdateEventArgs args)

   public virtual void OnInventoryUpdated(object source, InventoryUpdateEventArgs args)



After the changes are made to your price/inventory system, you can call to approriate method to raise an event. For example, to fire event in your price system implemenation, add this in your SetCatalogEntryPrices(IEnumerable<CatalogKey> catalogKeys, IEnumerable<IPriceValue> priceValues) method:

    _broadcaster.OnPriceUpdated(<list of prices changed>, new PriceUpdateEventArgs(catalogKeys.ToList()));



_broadcaster is an instance of CatalogKeyEventBroadcaster, which can be injected to your class via constructor dependency.


We're listening.

Those new API:s are marked as BETA, becase while we're quite happy with it, we want to hear more feedbacks from you. Let's know what you do want and what you don't, so we can adapt and make better API:s for you.

 

Jul 23, 2015

Comments

Praful Jangid
Praful Jangid Aug 11, 2021 04:03 AM

Thanks Quan,

Little older post but still it saved my day. I had caching issue for pricing update and this post helped me a lot. :)

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024