I have created a custom benefit/action for a promotion. When user applies the promo, nothing happens. There are no errors when i check in debug. The code is hit though.
public void Execute(IRuleExecutionContext context)
{
.....code
......
ApplyDiscountPercentForLineItems(commerceContext, validCartLineItems, discountPercent,
awardingBlockName);
}
public static void ApplyDiscountPercentForLineItems(CommerceContext commerceContext,
List<CartLineItem> validCartLineItems, decimal discountPercent,
string awardingBlockName)
{
var cart = (commerceContext.Objects.OfType<Cart>()).FirstOrDefault();
var totals = (commerceContext.Objects.OfType<CartTotals>()).FirstOrDefault();
var model = commerceContext.Objects.OfType<PropertiesModel>().FirstOrDefault();
foreach (var line in validCartLineItems)
{
var discount = commerceContext.GetPolicy<KnownCartAdjustmentTypesPolicy>().Discount;
var d = discountPercent;
if (commerceContext.GetPolicy<GlobalPricingPolicy>().ShouldRoundPriceCalc)
{
d = decimal.Round(d, commerceContext.GetPolicy<GlobalPricingPolicy>().RoundDigits,
commerceContext.GetPolicy<GlobalPricingPolicy>().MidPointRoundUp ?
MidpointRounding.AwayFromZero : MidpointRounding.ToEven);
}
decimal amount;
var currentAmount = totals.Lines[line.Id].SubTotal.Amount;
amount = currentAmount - (currentAmount * d / 100);
amount = amount * decimal.MinusOne;
totals.Lines[line.Id].SubTotal.Amount += amount;
var item = new CartLineLevelAwardedAdjustment
{
Name = (string)model.GetPropertyValue("PromotionText"),
DisplayName = (string)model.GetPropertyValue("PromotionCartText"),
Adjustment = new Money(commerceContext.CurrentCurrency(), amount),
AdjustmentType = discount,
IsTaxable = false,
AwardingBlock = awardingBlockName,
IncludeInGrandTotal = true
};
line.Adjustments.Add(item);
line.GetComponent<MessagesComponent>().AddMessage(
commerceContext.GetPolicy<KnownMessageCodePolicy>().Promotions,
$"PromotionApplied: {model.GetPropertyValue("PromotionId")}");
}
}