Using The New Promotions Engine in Episerver Commerce

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.

The other new feature from this release is the advent of the IPromotion engine. The IPromotionEngine is the new way to apply discounts to your cart.  Gone of the days of old workflow projects and in with a nice new interface friendly repository engine.

Creating our promotion

[ContentType(
DisplayName = "Promotion Type",
GUID = "47b72124-8257-4953-a278-c033ed088426",
Description = "Promotion Type")]
public class PromotionType : EntryPromotion
{
[Display(
Name = "Random Property",
Order = 100)]
[Required]
[CultureSpecific]
public virtual string RandomProperty{ get; set; }
}

The first thing we do is create a class that inherits from EntryPromotion. EntryPromotion inherits from IContent so you can define your promotion the same way you do a block or page.

The promotions engine will not pick up the promotion unless it has a processor.  To create a processor we inherit from PromotionProcessorBase passing in the custom EntryPromotion we defined above. THe processor is the thing that will be called when the promotion engines run method is called. In here you will need to add your logic to determine if the lineitems match the rules for your discount.

public class PromotionProcessor : PromotionProcessorBase<PromotionType>
{
public override IPromotionResult Evaluate(IOrderGroup orderGroup, PromotionType promotionData)
{
return new MyCustomPromotion();
}
}

The last part of the puzzle is to create a result.  In the result we do the actual logic of what discount should be applied to the basket.

public class MyCustomPromotion : IPromotionResult
{
public string Description
{
get
{
return string.Empty;
}
}
public FulfillmentStatus Status
{
get
{
return FulfillmentStatus.AlmostFulfilled;
}
}
public IEnumerable<PromotionInformation> ApplyReward()
{
return null;
}
}

Applying Promotions

public class ApplyPromotions
{
public Money ApplyPromotion(IOrderRepository orderRepository, IPromotionEngine promotionEngine)
{
var cart = orderRepository.Create<Cart>(Guid.NewGuid(), Cart.DefaultName);
var orderGroup = ((IOrderGroup)cart);
promotionEngine.Run(orderGroup);
cart.AcceptChanges();
}
}

Now in our code when we come to add a new line item or when we checkout, we can run the promotions against the basket.  This can be done by 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 out. When I tried implmenting this I found a lot of issues being thrown from the promotions engine run method for having too more than one content reference for a promotion in my discounted list. 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 you basket it and delete as required:

delete_episerver_commerce_basket

Conclusion

After trying to implement this in beta it definitely wasn’t as straight forward as I had hoped, especially with the lack of documentations (only to be expected for a beta feature). When you get everything up and running adding promotions in this way is a lot simpler and the code is a lot more obvious what it is doing.

If I had the option I would avoid using the old commerce workflows as much as possible and use this approach.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

1 reply

Trackbacks & Pingbacks

  1. […] some previous articles I’ve talked about Using The New Promotions Engine in EpiServer Commerce and The New Order Repository In Commerce. Also don’t forget you need to read this as well How […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *