Skip to content

Commerce Promotion API Reference

advanced
📜Corecommerce

The Promotion API evaluates and applies discounts to carts and orders. IPromotionEngine runs promotion evaluation. Custom promotions extend EntryPromotion, OrderPromotion, or ShippingPromotion.

Optimizely.Commerce.Marketing

TypePurpose
IPromotionEngineEvaluates promotions against a cart
PromotionDataBase class for all promotions
EntryPromotionDiscount on specific catalog entries
OrderPromotionDiscount on the entire order
ShippingPromotionDiscount on shipping
PromotionProcessorBaseBase class for custom promotion processors

Evaluates all active promotions against a cart.

Parameters

NameTypeDescription
orderGroupIOrderGroupCart to evaluate
settingsPromotionEngineSettingsEvaluation options

Returns: IEnumerable<RewardDescription>

PropertyTypeDescription
ExclusionLevelExclusionLevelNone, Unit, Order, Shipping
ApplyRewardboolWhether to apply the discount to the cart
RequestedStatusesRequestFulfillmentStatusAll, Fulfilled, NotFulfilled
Evaluate promotions on a cart
csharp
public class PromotionService
{
  private readonly IPromotionEngine _promotionEngine;

  public PromotionService(IPromotionEngine promotionEngine)
  {
      _promotionEngine = promotionEngine;
  }

  public IEnumerable<RewardDescription> ApplyPromotions(ICart cart)
  {
      var settings = new PromotionEngineSettings
      {
          ApplyReward = true,
          ExclusionLevel = ExclusionLevel.Unit
      };

      return _promotionEngine.Run(cart, settings);
  }
}
PropertyTypeDescription
PromotionPromotionDataThe promotion that generated this reward
SavedAmountdecimalAmount saved by this reward
StatusFulfillmentStatusFulfilled, NotFulfilled, PartiallyFulfilled
DescriptionstringHuman-readable reward description
RewardTypeRewardTypeEachAffectedEntry, WholeOrder, Shipping
PercentagedecimalPercentage discount (if applicable)

Discounts applied to specific line items.

PropertyTypeDescription
ConditionPromotionConditionWhen the promotion triggers
DiscountTargetCatalogItemSelectionWhich entries receive the discount
DiscountPromotionDiscountDiscount amount or percentage
CouponCouponDataOptional coupon requirement

Discounts applied to the order total.

PropertyTypeDescription
ConditionOrderPromotionConditionMinimum spend or item count
DiscountOrderDiscountOrder-level discount

Discounts applied to shipping costs.

PropertyTypeDescription
ConditionPromotionConditionTrigger condition
ShippingMethodsIList<Guid>Eligible shipping methods
DiscountShippingDiscountShipping discount
Custom percentage-off entry promotion
csharp
[ContentType(
  DisplayName = "Buy X Get Y Free",
  GUID = "a1b2c3d4-e5f6-7890-abcd-ef1234567890")]
public class BuyXGetYPromotion : EntryPromotion
{
  [Display(Name = "Required quantity")]
  public virtual int RequiredQuantity { get; set; }

  [Display(Name = "Free quantity")]
  public virtual int FreeQuantity { get; set; }
}
Custom promotion processor
csharp
public class BuyXGetYProcessor : EntryPromotionProcessorBase<BuyXGetYPromotion>
{
  protected override RewardDescription Evaluate(
      BuyXGetYPromotion promotion,
      PromotionProcessorContext context)
  {
      var entries = context.EntryPrices;
      var qualifyingEntry = entries
          .FirstOrDefault(e => e.Quantity >= promotion.RequiredQuantity);

      if (qualifyingEntry == null)
      {
          return RewardDescription.CreateNotFulfilled("Quantity not met.");
      }

      var discount = qualifyingEntry.Price * promotion.FreeQuantity;

      return RewardDescription.CreateFulfilled(
          discount,
          promotion,
          "Buy X Get Y applied.");
  }
}
PropertyTypeDescription
CodestringSingle coupon code
MaxRedemptionsintMaximum total uses
UsedRedemptionsintCurrent redemption count
PropertyTypeDescription
NamestringPromotion display name
IsActiveboolWhether the promotion is active
ValidFromDateTime?Start date
ValidUntilDateTime?Expiration date
PriorityintEvaluation priority (lower = higher priority)
ExclusionLevelExclusionLevelExclusivity level
BannerContentReferencePromotional banner content
ExceptionCause
PromotionProcessorExceptionError during promotion evaluation
CouponAlreadyUsedExceptionCoupon has reached max redemptions
InvalidPromotionExceptionPromotion configuration is invalid