public void Execute(IRuleExecutionContext context) { var commerceContext = context.Fact <CommerceContext>(null); var cart = commerceContext?.GetObject <Cart>(); var totals = commerceContext?.GetObject <CartTotals>(); if (cart == null || !cart.Lines.Any() || totals == null || !totals.Lines.Any() || SubtotalOperator == null || Subtotal == null || PercentOff == null) { return; } var percentOff = PercentOff.Yield(context); if (percentOff == 0) { return; } var matches = this.MatchingLines(context); if (matches == null || !matches.Any()) { return; } var list = matches.Where(l => SubtotalOperator.Evaluate(l.Totals.SubTotal.Amount, Subtotal.Yield(context)) && l.Quantity != decimal.Zero).ToList(); if (!list.Any()) { return; } var className = this.GetType().Name; var propertiesModel = commerceContext.GetObject <PropertiesModel>(); var discountAdjustmentType = commerceContext.GetPolicy <KnownCartAdjustmentTypesPolicy>().Discount; foreach (var line in list) { var discountAmount = percentOff * 0.01M * totals.Lines[line.Id].SubTotal.Amount; if (commerceContext.GetPolicy <GlobalPricingPolicy>().ShouldRoundPriceCalc) { discountAmount = decimal.Round(discountAmount, commerceContext.GetPolicy <GlobalPricingPolicy>().RoundDigits, commerceContext.GetPolicy <GlobalPricingPolicy>().MidPointRoundUp ? MidpointRounding.AwayFromZero : MidpointRounding.ToEven); } discountAmount *= decimal.MinusOne; line.Adjustments.Add(new CartLineLevelAwardedAdjustment() { Name = (propertiesModel?.GetPropertyValue("PromotionText") as string ?? discountAdjustmentType), DisplayName = (propertiesModel?.GetPropertyValue("PromotionCartText") as string ?? discountAdjustmentType), Adjustment = new Money(commerceContext.CurrentCurrency(), discountAmount), AdjustmentType = discountAdjustmentType, IsTaxable = false, AwardingBlock = className }); totals.Lines[line.Id].SubTotal.Amount = totals.Lines[line.Id].SubTotal.Amount + discountAmount; line.GetComponent <MessagesComponent>().AddMessage(commerceContext.GetPolicy <KnownMessageCodePolicy>().Promotions, string.Format("PromotionApplied: {0}", propertiesModel?.GetPropertyValue("PromotionId") ?? className)); } ; }