Shannon Gray
Oct 25, 2016
(5 votes)

Calculating Discounted Pricing With Episerver’s New Promotion Engine

Episerver's new promotion engine makes it easy to calculate discounts. It is also designed so that developers can easily override built-in promotion functionality and custom business rules can be applied in the calculation of discounts.

In working with Episerver's new promotion engine, the two most common places where you need to calculate discount prices are 1) for individual SKUs (like on a product detail page or category browse page) and 2) in the cart.

Calculating discounts and applying them to the cart is simple. Simply run either the CartValidate or CartPrepare activity flows as documented here:
Or you could use the more direct cart processing methods like cart.ApplyDiscounts() as documented here:

However, calculating the discounted price for a SKU/package individually involves a little more effort. Here's a quick and easy example discount calculator to do that:


private static Injected<ICurrentMarket> _currentMarket;
private static Injected<IPromotionEngine> _promotionEngine;
private static Injected<ReferenceConverter> _referenceConverter;
private static Injected<ILineItemCalculator> _lineItemCalculator;
public static decimal GetDiscountPrice(VariationContent variation, IMarket market)
  if (market == null)    
    market = _currentMarket.Service.GetCurrentMarket();    
  //these discounts are returned in order of priority and only if not excluded
  IEnumerable<DiscountedEntry> discountEntries =
    _promotionEngine.Service.GetDiscountPrices(new List<ContentReference>() { variation.ContentLink }, market, market.DefaultCurrency, _referenceConverter.Service, _lineItemCalculator.Service);    
  //get the biggest discount promotion application and return    
  decimal lowestDiscountedPrice = discountEntries.SelectMany(x => x.DiscountPrices).OrderBy(x => x.Price).FirstOrDefault().Price.Amount;    
  return lowestDiscountedPrice;


An explanation of what this code does:
_promotionEngine.Service.GetDiscountPrices() retrieves a list of DiscountedEntry objects. Each DiscountedEntry represents the discounted price for a SKU/package after each applicable promotion is applied. The DiscountedEntry instances reflect the applicable promotions with configured exclusions preventing inapplicable promotion discounts from being returned. In addition, the discounted prices are returned in the sort order specified in the promotion configuration UI for applicable promotions.

This means, when lowestDiscountedPrice is calculated, we're limited to taking one of the discounted prices and returning it. There isn't sufficient information readily available to calculate the compounded discount price for a SKU or package (when multiple promotions are applied).

GetDiscountPrices() also has a built-in price calculator to determine the sale price for the SKU or package (which is then discounted based on applicable promotions). The price calculator takes the lowest All Customers price from the SKU/packages.

If these two limitations work for your site (Only one promotion can apply to a SKU or package AND the lowest All Customers price is the correct SKU/package sale price), then this is all you need to use the new promotion engine on catalog pages. The approach is also a quick-and-easy way to test that custom promotions you're creating are functioning properly.

However, if this approach doesn't meet your site needs, some base functionality in GetDiscountPrices() needs to be overridden. This will be the topic of my next blog.

Oct 25, 2016


Please login to comment.
Latest blogs
Delete a content – the right way

I stumped upon this blogpost Delete content directly from the Optimizely database – Tomas Hensrud Gulla by Tomas, an OMVP. While the post is helpfu...

Quan Mai | Feb 8, 2023 | Syndicated blog

The Content Revolution - Generative AI

Hi Optimizely Community,  For the past two decades, you and Optimizely have been at the forefront of content management - creating, managing,...

Alex Atzberger | Feb 6, 2023

Delete content directly from the Optimizely database

Do you know your way around the Optimizely CMS database? Deleting, or updating, content directly in the database is pretty straightforward, but of...

Tomas Hensrud Gulla | Feb 6, 2023 | Syndicated blog

Rolling your site out to China

How to rollout your website to the China market Continue reading →

Andrew Markham | Feb 5, 2023 | Syndicated blog