K Khan
Jul 20, 2016
  3989
(2 votes)

Custom promotion engine

Hypothesis: Promotion engine will run promotions only for the defined market even if editors are creating promotions for other markets also.

using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer.Commerce.Marketing;
using EPiServer.Commerce.Marketing.Internal;
using EPiServer.Commerce.Order;
using EPiServer.Core;

namespace EPiServer.Reference.Commerce.Site.Features.Discounts.BrandBased
{
    public class CustomPromotionEngine : PromotionEngine
    {
        private readonly PromotionEngineContentLoader promotionEngineContentLoader;
        private readonly Lazy<ContentReference> campaignFolderRootLink;
        private readonly IRedemptionLimitService redemptionLimitService;
        private readonly PromotionFilters promotionFilters;
        private readonly PromotionApplicator promotionApplicator;
        private readonly ILineItemCalculator lineItemCalculator;
        private readonly IShippingCalculator shippingCalculator;
        private readonly IOrderFormCalculator orderFormCalculator;
        private readonly IContentLoader contentLoader;

        public CustomPromotionEngine(PromotionEngineContentLoader promotionEngineContentLoader, IRedemptionLimitService redemptionLimitService, PromotionFilters promotionFilters, PromotionApplicator promotionExecutor, ILineItemCalculator lineItemCalculator, IShippingCalculator shippingCalculator, IOrderFormCalculator orderFormCalculator, IContentLoader contentLoader) :
            base(promotionEngineContentLoader, redemptionLimitService, promotionFilters, promotionExecutor, lineItemCalculator, shippingCalculator, orderFormCalculator)
        {
            this.promotionEngineContentLoader = promotionEngineContentLoader;
            this.campaignFolderRootLink = new Lazy<ContentReference>(new Func<ContentReference>(this.GetCampaignFolderRoot));
            this.redemptionLimitService = redemptionLimitService;
            this.promotionFilters = promotionFilters;
            this.promotionApplicator = promotionExecutor;
            this.lineItemCalculator = lineItemCalculator;
            this.shippingCalculator = shippingCalculator;
            this.orderFormCalculator = orderFormCalculator;
            this.contentLoader = contentLoader;
        }

        public override IEnumerable<RewardDescription> Run(IOrderGroup orderGroup, PromotionEngineSettings settings)
        {
            if (orderGroup.Market.MarketName == "United Kingdom")
            {
                return base.Run(orderGroup, settings);
            }
            else
            {
                return Enumerable.Empty<RewardDescription>();
            }
        }

        }
}

Add registry for your custom promotion engine

c.For<IPromotionEngine>().Use<CustomPromotionEngine>();

Code was tested in quick silver

Jul 20, 2016

Comments

Please login to comment.
Latest blogs
Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024

How to write a bespoke notification management system

Websites can be the perfect vehicle for notifying customers of important information quickly, whether it’s the latest offer, an operational message...

Nicole Drath | Sep 25, 2024

Optimizely DAM – An Introduction

I presented a talk about the Optimizely DAM at the OMVP summit during Opticon 2024 in Sweden. I have now converted that talk into two blog posts....

Andrew Markham | Sep 25, 2024 | Syndicated blog

Simple and Effective Personalization with Optimizely Data Platform (ODP)

As we dive into the amazing capabilities of Optimizely One, let’s shine a spotlight on the Optimizely Data Platform (ODP). This simple tool unifies...

Alex Harris - Perficient | Sep 24, 2024 | Syndicated blog