Video: Coupon Hooks

Welcome! Join us as we learn about the Coupons and Coupon Hooks.

Skip to [0:28] Corporate Admin Coupon Page Overview

Skip to [3:43] Example of how to use a Coupon in an Order

Skip to [15:32] Code examples of Coupon Hooks

Skip to [17:38] Other uses for Coupon Hooks



Data Tables for Coupons

  • ORD_Promotions - Skip to [4:58].
  • ORD_CouponUsage joins to ORD_OrderTotals then Orders - skip to [6:15].

Global Auto Apply Coupons

This can be done on the Corporate Admin Coupons page if you create a coupon without a name.



Example: ProcessCouponCodes Hooks

For more information, skip to [8:27].

public OrderCoupons ProcessCouponCodesAfter(OrderCoupons currentCoupon, string[] couponCodes, int regionId, double subTotal, string currency, List<InventoryLineItem> lineItems, double shippingCost, OrderType orderType, int associateId, List<TaxLineItem> taxItems)
{
  // This coupon is to be applied to AutoShip orders only
  if (orderType != OrderType.AutoShip)
  {
    return currentCoupon;
  }
  
  // Retrieve the Associate stats
  var associateStats = _statsProvider.GetAssociateStats(associateId, DateTime.Now.AddDays(-7));
  
  if (associateStats?.Stats != null)
  {
    // TODO: Need Keys for the KPIs used
    if (associateStats.Stats.TryGetValue("PEKEY", out var personallyEnrolledStat) && associateStats.Stats.TryGetValue("PCPVKEY", out var totalPcPvStat))
    {
      // Calculate the earned 'Refer 3' discount
      bool alreadyUsed = false;
      var usedcoupons = ServiceLocator.CouponService.GetAssociateCouponUsage(associateId);
      
      foreach (var usedcoupon in usedcoupons)
      {
        if (usedcoupon.Info.Code == Refer3CouponCode)
        {
          if (DateTime.Now.Month == usedcoupon.DateUsed.Month && DateTime.Now.Year == usedcoupon.DateUsed.Year)
          {
            alreadyUsed = true;
          }
        }
      }
      
      if (!alreadyUsed)
      {
        
        var autoShipDiscount = CalculateAutoShipDiscount(personallyEnrolledStat.Value, totalPcPvStat.Value);
        
        if (autoShipDiscount > 0)
        {
          
          // Only allow the order to be discounted as much as the subTotal
          double discount = autoShipDiscount;
          
          if (discount > subTotal)
          {
            discount = subTotal;
          }
          
          // Grab the REFER3 coupon
          
          var refer3Coupon = _couponService.GetCouponsByCode(Refer3CouponCode).FirstOrDefault();
          
          if (refer3Coupon == null)
          { 
            throw new Exception("Cannot Apply Earned Discounts to AutoShip Orders! Coupon 'REFER3' is missing.");
          }
          
          // update the coupon discount
          refer3Coupon.Discount = discount;
          
          // Add the coupon to UsedCoupons
          // This code currently wipes any other coupons... may be a problem in the future, but not now
          
          var orderCoupon = new OrderCoupon(refer3Coupon);
          currentCoupon.DiscountTotal = discount;
          currentCoupon.UsedCoupons = new[] { orderCoupon };
        }
      }
    }
  }
  
  return currentCoupon;
}

public OrderCoupons GetProcessCouponCodes(OrderCoupons currentCoupon, string[] couponCodes, int regionId, double subTotal, string currency, List<InventoryLineItem> lineItems, double shippingCost, OrderType orderType, int associateId, List<TaxLineItem> taxItems)
{
  var stats = ServiceLocator.StatsProvider.GetAssociateStats(associateId, DateTime.Now.AddDays(-7));
  
  if (stats != null && stats.Stats != null)
  {
    
    foreach (var stat in stats.Stats)
    {
      if (stat.Key == "FreeClub" || stat.Key == "FreeCC")
      {
        if (stat.Value.Value == 1)
        {
          bool alreadyUsed = false;
          var usedcoupons = ServiceLocator.CouponRepository.GetAssociateCouponUsage(associateId);
          
          foreach (var usedcoupon in usedcoupons)
          {
            if (DateTime.Now.AddDays(-28) < usedcoupon.DateUsed)
            {
              alreadyUsed = true;
            }
          }
          
          if (!alreadyUsed)
          {
            double discount = subTotal + shippingCost;
            
            foreach (var item in lineItems)
            {
              if (item.SkuID == 5879) //OND Member subscription
              {
                discount -= item.Cost * item.Quantity; //really should only be 1 on an order
              }
            }                                
            
            CouponInfo coupon = new CouponInfo();
            coupon.Discount = discount; //remaining QV
            coupon.MinQty = 1;
            coupon.Code = "FreeClub";
            coupon.BeginDate = DateTime.MinValue;
            coupon.EndDate = DateTime.MaxValue;
            coupon.CouponId = 1;
            coupon.Recurring = true;
            coupon.AmountType = AmountType.Amount;
            coupon.CouponType = CouponType.OrderDiscount;
            coupon.AllItemIdsRequired = false;
            coupon.OrderType = OrderType.AutoShip;
            OrderCoupon oCoupon = new OrderCoupon(coupon);
            currentCoupon.DiscountTotal = discount;
            currentCoupon.UsedCoupons = new OrderCoupon[] { oCoupon };
          }
        }
      }
    }
  }
  
  return currentCoupon;
}


Example: GetCouponAdjustedVolumeOverride Hook

For more information, skip to [14:22].

public CouponAdjustedVolume GetCouponAdjustedVolumeOverride(CouponAdjustedVolume volume, OrderTotal[] totals)
{
  foreach (OrderTotal total in totals)
  {
    // Update CV and QV if the 'Refer 3' coupon was used
    
    var usedCoupons = total.Coupons.UsedCoupons;
    
    if (usedCoupons != null && usedCoupons.Any(x => x.Info.Code.Equals(Refer3CouponCode, StringComparison.OrdinalIgnoreCase)))
    { 
      volume.Cv = 0;
      volume.Qv = 0;
    }
  }
  
  return volume;
}

Did this page help you?