Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Coupons

Describes how to manage coupon promotions in Optimizely Commerce Connect.

The default coupon system supports setting a single coupon code for any promotion. If set, a matching coupon code must be supplied for the order, or the promotion is not applied.

To change the default implementation, implement the following interfaces and register them as the default services in the service locator. For more information, see Dependency injection.

Classes in this topic are available in the following namespace:

  • EPiServer.Commerce.Marketing

Access an order's coupons through EPiServer.Commerce.Order.IOrderForm.CouponCodes. This is an ICollection<string>, which supports adding and removing coupon codes.

ICouponFilter

This interface has one method to implement. The method filters out promotions that do not meet the coupon requirements. The method returns a PromotionFilterContext, where the IncludedPromotions property contains all promotions that are still valid after coupons are evaluated. It also populates the PromotionCouponCodeMap property with information about applied coupon codes, and adds descriptions to ExcludedPromotionDescriptions for any excluded promotions.

PromotionFilterContext Filter(PromotionFilterContext filterContext, IEnumerable<string> couponCodes);
  • filterContext – Its IncludedPromotions property contains the list of promotions that the engine will evaluate against the current order.
  • couponCodes – The list of coupon codes supplied for the order.
  • PromotionFilterContext.IncludedItems – It should include all promotions that meet the coupon requirements. Two types of promotions are included: those that require a coupon and provide a valid one, and those that do not require a coupon. Eliminate promotions that require a coupon, but no valid one is supplied.

The following example shows a filter that verifies against a single coupon code:

public virtual PromotionFilterContext Filter(PromotionFilterContext filterContext, IEnumerable<string> couponCodes) {
  foreach(var promotion in filterContext.IncludedPromotions) {
    var couponCode = promotion.Coupon.Code;
    if (String.IsNullOrEmpty(couponCode)) {
      continue;
    }

    if (couponCodes.Contains(couponCode, StringComparer.OrdinalIgnoreCase)) {
      filterContext.AddCouponCode(promotion.ContentGuid, couponCode);
    } else {
      filterContext.ExcludePromotion(
        promotion,
        FulfillmentStatus.CouponCodeRequired,
        filterContext.RequestedStatuses.HasFlag(RequestFulfillmentStatus.NotFulfilled));
    }
  }
  return filterContext;
}

ICouponUsage

Implementing ICouponUsage is optional. Implement it to track applied coupon codes. The interface has one method to implement:

void Report(IEnumerable<PromotionInformation> appliedPromotions);

This method is called when a cart is saved as a purchase order. The method takes one parameter: a list of promotions (with and without coupon codes) applied to the purchase order. The following example implements ICouponUsage:

public class CustomCouponUsage : ICouponUsage
{
  public void Report(IEnumerable<PromotionInformation> appliedPromotions)
  {
    // Store any information needed about the coupon codes that were used.
  }
}