0

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")}");
 }           
}
1
  • 1
    What version are you using? I'm on 9.0.2 version and it doesn't have CartLineItem, but has CartLineComponent. Could you also check that validCartLineItems that you pass to a method is an actual data and you are not creating a new collection of items, that is not connected with context cart.
    – vzaverugo
    Commented Oct 3, 2018 at 18:35

1 Answer 1

1

I had this issue previously and it was because by my custom benefit was implementing IAction and not ICartLineAction.

As part of the benefit resolution process the IDiscoverPromotionsPipeline is executed and it contains the FilterPromotionsByBenefitTypeBlock, which excludes any benefits that dont implement ICartLineAction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.