Skip to content

Instantly share code, notes, and snippets.

@jstemerdink
Created February 28, 2017 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstemerdink/367579b47b288e212eb517da381a2ffe to your computer and use it in GitHub Desktop.
Save jstemerdink/367579b47b288e212eb517da381a2ffe to your computer and use it in GitHub Desktop.

Limiting applied promotions

A way to limit the total amount of discounts apllied.

Read my blog here

Powered by ReSharper image

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>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment