NOTE: THIS POST WAS WRITTEN WHEN PROMOTIONS WAS IN BETA. THE CODE CHANGED SLIGHTLY WHEN IT WAS RELEASED INTO PRODUCTION

In Episerver commerce 8.12 a new beta version of the promotion has been shipped. The good news is that the promotion engine is built against an interface, IPromotion. The IPromotionEngine is the new way to apply discounts to items in your cart. Gone are the days of the horrible old Episerver Commerce workflow project. Soon we will have a nice new interface friendly repository engine.

In my previous post 'The New Order Repository In Commerce' I went through some of the benefits of using the new order repository that comes with Episerver commerce 8.12.0. Today we will look at some code snippets of how the new promotion engine works 🔥🔥🔥

Creating A Promotion: Promotions are defined using EntryPromotion:

To get started with the new promotions engine, create a class that inherits from EntryPromotion. EntryPromotion inherits from IContent so you can define your promotions the same way you do a block or page, in cod using the special Episerver attributes. The promotions engine will not pick up the promotion unless it has a processor. To create a processor, create a class that inherits from PromotionProcessorBase. You will need to pass in the custom EntryPromotion to register it. The processor is the thing that will be called when the promotion engines Run() method is called. Here you will need to add your logic to determine if the line items match the rules for your discount. This is done using this code:

The last part of the puzzle is to create a result of type IPromotionResult. The actual logic of what discount should be applied to the basket will be up to you. It's your discount after all 😉

Finally, you will need to apply the promotion to the cart. This is done using this code:

Using this code, whenever a new item is added to the basket or when calculating the basket for checkout, the code will iterate through all the promotions and apply them against the basket. This step is done using the IPromotionEngine and passing in an IOrderGroup!

Clearing The Basket: When you start creating custom promotions you will very likely need to clear your basket from any previously applied promotions. When I tried implementing this using the beta, whenever I had more than one promotion applied I found a lot of issues being thrown from the Run() method, hopefully, these will be fixed soon 😕

If you do not know how to clear your basket, log into the Episerver commerce manager, click the commerce manager tab and select `order management. From here you should be able to find the basket and delete it as required:

Using The New Promotions Engine in Episerver Commerce


Trying to implement promotions in the beta definitely wasn't as straightforward as I had hoped, especially with the lack of documentation (only to be expected for a beta feature). When you get everything up and running, adding promotions using the new engine is a lot simpler and the code cleaner compared to the old way. If I had the option to start a new Commerce build now, I would avoid using the old Episerver Commerce workflows and use this new approach now. You may need to refactor some of it, however, it will likely be quicker and less pain-free. Happy Coding 🤘