I have a Campaign with a Visitor Group applied and one or more Discounts under this Campaign.
I'd like to use the PromotionEngine's Evaluate() method to fetch the discount price of a product in this specific Campaign. I am trying to do this during a Scheduled Job in EPiServer.
However using the Evaluate() method returns nothing. I assume this is because that the job that is running the evaluation is not a part of the Visitor Group.
How would I go about evaluating the discount price of a product where a visitor group is applied?
Your assumptioon is correct, the PromotioneEngine will evaluate it for the current context and if the current user isn't in your visitor group you won't get the result you want.
I fear you are out of luck though:https://world.episerver.com/forum/developer-forum/Feature-requests/Thread-Container/2019/9/evaluate-prices-for-specific-customer-or-visitor-groups/Unless something has changed that i am not aware of then there is no support for this scenario in episerver commerce, you would have to build your own promotions engine to add support for it.
Thank you Erik.
After trying quite a few different solutions I've managed to do it by authenticating the HttpContext.Current with a user that is a part of the visitor group.
This will work for me in my specific scenario but I can imagine it's not a proper solution in a lot of other cases.
If anyone is looking to do the same, this is how I did it:
I created an IDisposable class called AuthenticatedContext that
I then wrapped the Evaluate() method call in the AuthenticatedContext.
using (var _ = new AuthenticatedContext(_authenticationService,"username", "password"))
allDiscounts = _promotionEngine.Evaluate(variation.ContentLink, market, market.DefaultCurrency, RequestFulfillmentStatus.Fulfilled | RequestFulfillmentStatus.PartiallyFulfilled);
lineDiscounts = allDiscounts.Where(x => x.Promotion.DiscountType != DiscountType.Order);
The AuthenticatedContext code looks like this. (AuthenticationService is a custom class to this solution)
public AuthenticatedContext(IAuthenticationService authenticationService, string username, string password)
_authenticationService = authenticationService;
if (HttpContext.Current == null || !_authenticationService.IsAuthenticated())
HttpContext.Current = CreateMockHttpContext();
_authenticationService.AuthenticateAndLogin(username, password, true);
The CreateMockHttpContext() contains the code found here: https://gist.github.com/lkaczanowski/4174294
public void Dispose()
HttpContext.Current = null;