Promotion Events Development Guide
Enticify fires promotion events during the application of promotions. You can use these events to understand what Enticify is doing and take action on this information. You can ignore these events, or you can use them in your solution.
You can access events as they happen, or once they've all been fired.
OrderForm Event Collection
You can access all of the events that Enticify fired after the pipeline has run. Enticify stores an event collection on the OrderForm
.
Do the following to access the events collection:
- Add a reference to
Enticify.CommerceServer.dll
. - Add
using Enticify.Promotions
. - Call our extension method
GetPromotionApplicationEvents
on theOrderForm
.
Example - Promotion Distance Messages
In this example we look in the collection of PromotionEvent
instances for the first InsufficientConditionItems
event instance (if there is one). We can then use the data on this event to build our promotion distance messages for display in the basket or elsewhere.
using System.Collections.Generic; using System.Linq; using Enticify.Promotions; using Enticify.Promotions.Events; #if MS using Microsoft.CommerceServer.Runtime.Orders; #else using CommerceServer.Core.Runtime.Orders; #endif namespace Enticify.Samples { public class PromoEventProcessor { public IListGetBasketUpsellMessages(OrderForm orderForm) { //Get the event that tells us when the orderForm has some, //but not all of the condition items required for a promo. //We only get the first, as this will be from the highest //priority promo. Multiple, may lead to customer confusion. var insufficientConditionItems = orderForm.GetPromotionApplicationEvents() .OfType () .FirstOrDefault(); //Return if there was no such event. if (insufficientConditionItems == null) return new List (); //Get the list of messages for this partially qualified promo. //Use this information however you want. Here we just return //a simple list of messages that we can display to the customer. return #pragma warning disable 618 insufficientConditionItems .ConditionBasketItemUpsells #pragma warning restore 618 .Select(upsell => upsell.ShoutOut.Text.ToString()) .ToList(); } } }
Event Handlers
You can handle one, some or all of the Enticify Promo Events as they happen. You can create multiple handlers that handle the same events, but do different things with them.
Example: Handling PromotionPreRequisitesNotSatisfied Event
The PromotionPreRequisitesNotSatisfied
is fired when one or more of the promotion eligibility requirements are not met. You might use this event to track how often this occurs on your site. Do the following to handle this event:
- Add a reference to
Enticify.CommerceServer.dll
. - Add a new class called
EnticifyEventSinkThingy
(the name is not important). - Add the following namespace directives:
using Enticify.Promotions;
using Enticify.Promotions.Events;
using Enticify.Promotions.Events.Advanced;
- Implement the generice interface
IPromotionEventSubscriber<PromotionPreRequisitesNotSatisfied>
on this class. - Call one of the
ExtensionRegistry.ScanAssembliesForExtensions
overloads once in the application on start. This must happen before Enticify runs for the first time. This class is in theEnticify.Promotions.Advanced
namespace. In previous versions of Enticify, you would have usedEnticifyExtensions.RegisterFromAssemblies
. - You are done!
This would look as follows:
using System.Diagnostics; using Enticify.Promotions.Events; namespace Enticify.Samples { ////// Class that implements IPromotionEventSubscriber for InsufficientConditionItems. /// This will be called whenever InsufficientConditionItems is fired. /// public class PromotionPreRequisitesNotSatisfiedHandler : IPromotionEventSubscriber{ public void OnPromoApplicationEvent(PromotionPreRequisitesNotSatisfied promotionEvent) { var id = promotionEvent.Promotion.Id; Trace.WriteLine(string.Format("Promo Id {0} prerequisites not met.", id)); } }