As of EPiServer Commerce 8.15 Price events are now available in the core product. More information: http://world.episerver.com/blogs/Quan-Mai/Dates/2015/7/new-events-for-price-and-inventory-updates/
When developing a EPiServer Commerce solution it may be necessary to listen for events that let us know pricing has changed. By default EPiServer Commerce does not broadcast an event when prices change so I wanted to put a solution together that allowed developers to subscribe to events in a similar way that we can for content events in EPiServer.
So what use does this have? I can think of a number of uses where listening to events on pricing may be useful:
- Logging - Prices are sensitive and you can use price save events to log who changed the prices
- Indexing - If you are using a tool such as EPiServer Find you may be indexing price information with your product data. The price events allow you to re-index individual items as soon as the price as changed
- Integration - You may wish to kick off a process or send a message to an external system as soon a price changes
Ever since EPiServer 7 the Commerce engine has used price providers to interact with the underlying price pricing store. As the provider instances are created by StructureMap it gives us an opportunity so intercept the requests and broadcast an event about changes in prices. So I took some inspiration from Nimesh Manmohanlal's blog on Cross cutting concerns in EPiServer using Dynamic Proxies. I didn't want to create a custom pricing provider, just let event subscribers know when a price has been updated. All that's needed is to listen out for the methods that save prices, intercept those requests and broadcast an event.
The solution uses StructureMap and Castle interceptors to intercept methods that save prices. An events system is then implemented to broadcast the events and allow users to subscribe.
How to get PriceEvents for EPiServer Commerce
How to use PriceEvents for EPiServer Commerce
The most simple way to use it is to add the Nuget package to your solution (Commerce v8 upwards supported). If your users update prices in Commerce Manager don't forget to add to your Commerce manager project too. Then use an initialisation mode to listen out for the price changes:
Events will be broadcast whenever a price is changed across the system so you can listen in and take action as appropriate.
Seeing PriceEvents it in action
OK so demonstrating price events is a little tricky so I implemented the example code above and put together a simple page that refreshes every second and lists out the price changes:
I put this post together as a proof of concept so its not been through a test cycle. Every single price change will be broadcast via an event, so batch updates could generate a lot of events!
I tested it on the Commerce Starter kit but I don't take any responsibility for any issues caused by its usage. If you spot an issue let me know!
Have your solutions required this kind of behaviour? How have you addressed it in the past? Let me know on @davidknipe or in the comments below.