Shannon Gray
Nov 14, 2016
  3437
(2 votes)

Promotion Engine Sale Price Customization

Overriding how sale prices are calculated in Episerver’s promotion engine for SKUs and packages outside of the cart requires a minor customization. Good examples of where this applies are category landing page and product detail pages where you may show a discount price(s). Sales price calculation is usually done by a) evaluating the prices associated with a SKU or package and b) applying the site’s business rules to determine the correct price of an item for a customer. The sales price is calculated in the promotion engine to determine the price from which to subtract the promotion discounts. As I previously mentioned here, using the PromotionEngine GetDiscountPrices() method involves an important constraint: It has a default sale price calculation built in which may or may not fit your site’s business rules. Here is how to customize the promotion engine to use alternate pricing calculation logic.

The sale price for items to be evaluated in the promotion engine is calculated in the PromotionEngineContentLoader class by calling the ReadOnlyPricingLoader GetDefaultPrice() method. This method returns the lowest “All Customer” price for an item that has a valid date and is the correct currency and market for the current customer. So it doesn’t use customer-specific, price group, or custom sale price types to calculate the sale price. An instance of PromotionEngineContentLoader is stored in the IOC container and used in the promotion engine - so it can be overridden in your implementation.

To override this sale price calculation in the promotion engine, create a class that inherits from PromotionEngineContentLoader and then configure your IOC container to use this new implementation. Here’s an example of how you can do this:

   1: public class CustomPromotionEngineContentLoader : PromotionEngineContentLoader
   2: {
   3:     public CustomPromotionEngineContentLoader(
   4:             IContentLoader contentLoader, 
   5:             CampaignInfoExtractor campaignInfoExtractor, 
   6:             ReadOnlyPricingLoader readOnlyPricingLoader) : base(contentLoader, campaignInfoExtractor, readOnlyPricingLoader){ }
   7:  
   8:     public override IOrderGroup CreateInMemoryOrderGroup(ContentReference entryLink, IMarket market, Currency marketCurrency)
   9:     {
  10:         var orderGroup = new InMemoryOrderGroup(market, marketCurrency);
  11:  
  12:         //this is where you can reference your own pricing calculator to retrieve the right sale price
  13:         IPriceValue price = YourSalePriceCalculator.GetSalePrice(entryLink);
  14:  
  15:         if (price != null)
  16:         {
  17:             orderGroup.Forms.First().Shipments.First().LineItems.Add(new InMemoryLineItem
  18:             {
  19:                 Quantity = 1,
  20:                 Code = price.CatalogKey.CatalogEntryCode,
  21:                 PlacedPrice = price.UnitPrice.Amount
  22:             });
  23:         }
  24:  
  25:         return orderGroup;
  26:     }
  27:
  28: }

Notice that an IOrderGroup is used to contain the SKU or package. The InMemoryOrderGroup is simply an implementation of that interface that allows carts and one-off SKUs or packages to be processed for promotion calculation in the same way. This also simplifies promotion calculation. It allows for the custom promotion processors (see here) that you create to simply process the IOrderGroup instance rather than having separate methods for one-off items and carts. The PromotionProcessorContext class contains an IOrderGroup property and is passed into the Evaluate() method for promotion processor classes.

Finally, add initialization of your IOC container to point to your custom implementation like this:

   1: [ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
   2: public class DiscountCalculateInitialization : IInitializableModule, IConfigurableModule
   3: {
   4:     public void ConfigureContainer(ServiceConfigurationContext context)
   5:     {
   6:         context.Container.Configure(c => c.For<PromotionEngineContentLoader>().Singleton().Use<CustomPromotionEngineContentLoader>());
   7:     }
   8:
   9: }

The ModuleDependency ensures that the new implementation is set/used, overriding the original implementation set in Episerver’s commerce initialization module.

As you can see, overriding the promotion engine sale price takes very little effort. I think the new promotion engine has made calculating discounts a lot easier with a straight-forward and exposed model that can be overridden.

Nov 14, 2016

Comments

Erik Norberg
Erik Norberg Jun 21, 2017 03:16 PM

This is more complicated than it has to be, you can use the default PromotionEngineContentLoader there is no need to override it.

It uses the ReadOnlyPricingLoader.GetDefaultPrice which in turn calls IPriceService.GetDefaultPrice, which really is the one you need to override.

Write your own IPriceService with your custom logic in the GetDefaultPrice function and register our own IPriceService and the promotion engine will work the way you want.

Toke Frostholm
Toke Frostholm Sep 10, 2020 12:13 PM

@Erik can you provide an example of such implementation? I am struggling getting my overridden GetDefaultPrice to fire after calling IPromotionEngine.Run.

Please login to comment.
Latest blogs
Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog

ExcludeDeleted(): Prevent Trashed Content from Appearing in Search Results

Introduction In Optimizely CMS, content that is moved to the trash can still appear in search results if it’s not explicitly excluded using the...

Ashish Rasal | Nov 7, 2024

CMS + CMP + Graph integration

We have just released a new package https://nuget.optimizely.com/package/?id=EPiServer.Cms.WelcomeIntegration.Graph which changes the way CMS fetch...

Bartosz Sekula | Nov 5, 2024

Block type selection doesn't work

Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of...

Damian Smutek | Nov 4, 2024 | Syndicated blog

.NET 8 FAQ

I have previously written about .NET compatibility in general and .NET 8 in particular, see blog posts here , here and here . With the end of suppo...

Magnus Rahl | Nov 4, 2024

Dynamic packages in Commerce Connect

In Optimizely Commerce Connect, you can group different items using packages and bundles. Package: A package has one or more versions of a product...

K Khan | Nov 1, 2024