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

Evaluate discount with visitor group

Vote:
 

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?

#222205
Edited, Apr 30, 2020 9:39
Vote:
 

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.

#222217
Apr 30, 2020 13:24
Vote:
 

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 

  1. Creates a Mock HttpContext
  2. Sets the HttpContext.Current to the Mock HttpContext
  3. Logs in the user
  4. Sets HttpContext = null on Dispose()

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;
        }
#222218
Edited, Apr 30, 2020 13:46
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.