Try our conversational search powered by Generative AI!

Find out if product(s) is part of campaign


Is there a efficient way to find out if a product/variant is part of a campaign/promotion? For example, lets say I want to know this on a product listing page containing like 20 products.

My best bet would be to use GetPromotionItemsForCampaign(...), but I'm not sure how to use in a efficient way. Probably I want to batch it in some way.

An other suggestion would be to run _promotionEngine.Evaluate(...) and if that returns a partially fulfilled promotion, then maybe I can consider it part of that campaign?

Oct 22, 2019 9:51
Andreas J - Oct 23, 2019 9:54
I would say no - `GetDiscountPrices` uses `Evaluate`, so it would be more efficient to use it directly I think.

If you just want to check if the product has a discounted value, then you can do as

bool hasDiscount = Model.IsAvailable && Model.DiscountedPrice.GetValueOrDefault().Amount < Model.PlacedPrice.Amount;

@model EPiServer.Reference.Commerce.Site.Features.Shared.Models.IProductModel
bool hasDiscount = Model.IsAvailable && Model.DiscountedPrice.GetValueOrDefault().Amount < Model.PlacedPrice.Amount;

The above code sample is from QuickSilver demo site. (_Product.cshtml)

Oct 23, 2019 10:32
Andreas J - Oct 23, 2019 11:02
Your sample code is just an abstraction and says nothing about how the discounted price is actually resolved. In the end it uses `_promotionEngine.GetDiscountPrices(...)` and you don't bring anything new to this thread.

Evaluate is the way to go:

_promotionEngine.Evaluate(variant,market,market.DefaultCurrency,RequestFulfillmentStatus.Fulfilled | RequestFulfillmentStatus.PartiallyFulfilled);

You can also call the Run method directly if you think that might be more effecient, but i doubt it will be noticeable.

Even then it isn't really fast enough to be used on a page load and even less so for a listing, the result needs to be cached, if you already use a search engine for the listing you can call evaluate when doing the indexing and store the result in there. As with all caching don't forget to handle the invalidation.

Oct 23, 2019 14:55

There are of course more pitfalls as the result from Evaluate will contain promotions you probably aren't interesting in, as a start I would suggest you filter down to LineItem discounts without an code:

_promotionEngine.Evaluate(variant, market, market.DefaultCurrency, RequestFulfillmentStatus.Fulfilled | RequestFulfillmentStatus.PartiallyFulfilled)
                                    .Where(x => x.Promotion.DiscountType == DiscountType.LineItem && string.IsNullOrEmpty(x.Promotion.Coupon.Code)).ToList();
Oct 23, 2019 15:03
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.