November Happy Hour will be moved to Thursday December 5th.

Marketing [Beta] 9.15.0 doen't evaluate promotions

Vote:
 

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
{
	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(), 
				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
Vote:
 

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
Vote:
 
<p>Thanks Quan! Fixed and it works. Only correct version is this:</p> <pre class="brush:csharp;auto-links:false;toolbar:false" contenteditable="false">var redemptions = new[] {new RedemptionDescription(new AffectedOrder(orderForm)) };</pre> <p>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.</p> <p>What is&nbsp;GetPromotionItems method for and is it ok to return null? If it is not used in all processors, maybe it should not be abstract?</p>
#148666
May 19, 2016 15:34
Vote:
 

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
Vote:
 
<p>Thanks! Waiting for more improvements in an API.</p>
#148668
May 19, 2016 15:54
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.