A way to limit the total amount of discounts apllied.
Read my blog here
private IEnumerable<RewardDescription> ApplyDiscounts(ICart cart) | |
{ | |
IOrderForm orderForm = cart.GetFirstForm(); | |
if (orderForm == null) | |
{ | |
return new List<RewardDescription>(); | |
} | |
decimal total = orderForm.Shipments.Sum(shipment => shipment.LineItems.Sum(item => item.PlacedPrice * item.Quantity)); | |
if (total <= 0) | |
{ | |
return new List<RewardDescription>(); | |
} | |
List<ContentReference> items = new List<ContentReference>(); | |
foreach (IShipment shipment in orderForm.Shipments) | |
{ | |
items.AddRange(shipment.LineItems.Select(li => li.GetEntryContent().ContentLink)); | |
} | |
IEnumerable<RewardDescription> rewards = this._promotionEngine.Evaluate(items, this._currentMarket.GetCurrentMarket(), cart.Currency, RequestFulfillmentStatus.Fulfilled); | |
decimal totalDiscount = rewards.Sum(rewardDescription => rewardDescription.SavedAmount); | |
return totalDiscount <= (total / 2) ? cart.ApplyDiscounts(this._promotionEngine, new PromotionEngineSettings()) : new List<RewardDescription>(); | |
} |