Marketing [Beta] 9.15.0 doen't evaluate promotions

Member since: 2012

I have just upgraded EPiServer Commerce from 9.8.1 to 9.15.0. I am using custom promotion and found that API for promotions has been changed.

Here is the current code for custom OrderPromotion:

[ContentType(GUID = "E6271950-DB98-4FE6-9626-CEFCBF46BE19")]
public class AdditionalDiscountPromoData : OrderPromotion
{
}

public class AdditionalDiscountPromoProcessor : PromotionProcessorBase<AdditionalDiscountPromoData>
{
	protected override RewardDescription Evaluate(
		AdditionalDiscountPromoData promotionData,
		PromotionProcessorContext context)
	{
		var orderForm = context.OrderForm;
		var cart = context.OrderGroup as Cart;
		if (cart == null)
		{
			return NoReward(promotionData);
		}

		var additionalDiscountPercent = 10.0m;

		if (!orderForm.Shipments.Any()) // Shipment gets removed when no items in the cart
		{
			return NoReward(promotionData);
		}

		var redemptions = orderForm.Shipments
			.First()
			.LineItems
			.Select(lineItem => new AffectedItem(lineItem))
			.Select(affectedItem => new RedemptionDescription(affectedItem));

		return RewardDescription.CreatePercentageReward(
			FulfillmentStatus.Fulfilled,
			redemptions,
			promotionData,
			additionalDiscountPercent,
			description: $"{additionalDiscountPercent} % discount applied to an order");
	}

	private RewardDescription NoReward(PromotionData promotionData)
	{
		return new RewardDescription(
				FulfillmentStatus.NotFulfilled, 
				Enumerable.Empty<RedemptionDescription>(), 
				promotionData, 
				unitDiscount: 0,
				unitPercentage: 0, 
				rewardType: RewardType.None, 
				description: "No discount applied");
	}

	protected override PromotionItems GetPromotionItems(AdditionalDiscountPromoData promotionData)
	{
		return null;
	}
}

There is one new method to override "GetPromotionItems" I don't know how to use - just returning null.

Another change compared to previous version is that instead of afftected items RewardDescription expects RedemptionDescription sequence. RedemptionDescription constructor can take 1 or more affected items. I am just mapping affected item to RedemptionDescription one to one.

I went through with debugger and seems that Evaluate method is executed correctly. GetPromotionItems method is never called. But discount doesn't get applied on the order anymore. Previous version work perfectly.

What am I missing?

#148660 May 19, 2016 14:44
  • Member since: 2011

    This should be changed

           var redemptions = orderForm.Shipments
                .First()
                .LineItems
                .Select(lineItem => new AffectedItem(lineItem))
                .Select(affectedItem => new RedemptionDescription(affectedItem));

    it should be

    redemptions = new List<RedemptionDescription>() { new RedemptionDescription(new AffectedOrder(orderForm)) };
    New version of the promotion system come with three types of AffectedObjectBase: AffectedItem (for EntryPromotion), AffectedOrder (for OrderPromotion) and AffectedShipment (for ShippingPromotion)
    Regards,
    /Q
    #148665 Edited, May 19, 2016 14:58
  • Member since: 2012

    Thanks Quan! Fixed and it works. Only correct version is this:

    var redemptions = new[] {new RedemptionDescription(new AffectedOrder(orderForm)) };

    Also it would be good if API would not allow invalid use. For example, OrderPromotion processors should expect AffectedOrder redemptions, EntryPromotion processor should expect AffectedItems etc. Then API would guide developer even without documentation.

    What is GetPromotionItems method for and is it ok to return null? If it is not used in all processors, maybe it should not be abstract?

    #148666 May 19, 2016 15:34
  • Member since: 2011

    Hi,

    It should return the items which the promotion applies to. For an order promotion you can return this:

    return
        new PromotionItems(
            promotionData,
            new CatalogItemSelection(nullCatalogItemSelectionType.All, true),
            new CatalogItemSelection(nullCatalogItemSelectionType.All, true));


    #148667 May 19, 2016 15:46
  • Member since: 2012

    Thanks! Waiting for more improvements in an API.

    #148668 May 19, 2016 15:54