Using ProductPriceModelBuilder in scheduled task

We need to calculate the price (list & campaign) for a variant in a scheduled task, but the ProductPriceModelBuilder throws an exception because it “Cant fetch OrderCarrier from users session” (sic) which sounds like it needs a user context.

Is there a way to use the ProductPriceModelBuilder without a user context, e.g., from a scheduled task?

Or is there something other than the ProductPriceModelBuilder that can calculate the list & campaign price for a variant with a user context?

Litium version: 8.6.1

ProductPriceModelBuilder is using the user context. You have to use directly IPriceCaclulator and IDiscountPriceCalculator.

We have tried to use the ICampaignPriceCalculator (this is Litium 7), but it doesn’t seem to be able to calculate any campaign prices. The list of campaign prices it calculates is always empty.

We are using the accelerator ICampaignPriceCalculator implementation.

But it’s supposed to work?

Yes, we are using it when we are creating the price agent feeds via scheduled jobs. Be sure that your CampaignPriceCalculatorArgs is correct.

1 Like

Pass in the OrderCarrier in the CampaignPriceCalculatorArgs.OrderCarrier, if that is null the system tries to resolve the OrderCarrier from the user http session.

1 Like

Not sure what “the OrderCarrier” refers to. But this is what we do currently:

_orderCarrier = orderFactory.Create((OrderCarrier)null, Solution.Instance.AnonymousToken);
// ...
var priceCalculatorArgs = new CampaignPriceCalculatorArgs() {
    VariantSystemId = variant.SystemId,
    CurrencySystemId = currency.SystemId,
    DateTimeUtc = DateTime.UtcNow,
    LanguageID = _languageService.Get(cultureInfo).SystemId,
    Quantity = 1M,
    SecurityToken = SecurityToken.AnonymousToken,
    ChannelId = channel.SystemId,
    OrderCarrier = _orderCarrier,
    CountrySystemId = channelToCountryLink.CountrySystemId,
    SourceId = "scheduled task"
};
var campaignPriceResults = _campaignPriceCalculator.GetCampaignPrices(priceCalculatorArgs);

However, campaignPriceResults is always empty when we do this.

We have double-checked that all the calculator arguments are correct, and that product pages on the site show campaign prices.

We have created campaigns that apply to all channels and all currencies just for testing, but regardless, there are never any campaign price results returned.

Have you checked that you do not have tier prices and that the campaign that you set is a product campaign without any extra conditions.

This is the code in the price agent factory for Litium 7 that fetching the product list and campaign prices in a scheduled task.

            var priceCalculatorArgs = new PriceCalculatorArgs
            {
                CurrencySystemId = currencyId,
                DateTimeUtc = DateTimeOffset.UtcNow,
                WebSiteSystemId = webSite.SystemId,
                CountrySystemId = priceAgentItemArgs.CountryId,
                SourceId = "price agent"
            };

            var priceCalculatorItemArgs = new PriceCalculatorItemArgs
            {
                VariantSystemId = variant.SystemId,
                Quantity = 1
            };

            var campaignPriceCalculatorArgs = new CampaignPriceCalculatorArgs
            {
                VariantSystemId = variant.SystemId,
                CurrencySystemId = currencyId,
                DateTimeUtc = DateTime.UtcNow,
                LanguageID = _languageService.Get(culture).SystemId,
                Quantity = 1,
                SecurityToken = SecurityToken.AnonymousToken,
                ChannelId = channel.SystemId,
                OrderCarrier = _orderCarrier,
                CountrySystemId = _orderCarrier.CountryID,
                SourceId = "price agent"
            };

            if (!_priceCalculator.GetListPrices(priceCalculatorArgs, priceCalculatorItemArgs)
                .TryGetValue(priceCalculatorItemArgs.VariantSystemId, out var listPrice))
            {
                return priceAgentItemResult;
            }
            
            priceAgentItemResult.PriceWithVat = listPrice.ListPriceWithVat;
            priceAgentItemResult.VatPercentage = listPrice.VatPercentage;

            if (_campaignPriceCalculator.GetCampaignPrices(campaignPriceCalculatorArgs).TryGetValue(campaignPriceCalculatorArgs.VariantSystemId, out var campaignPrice))
            {
                priceAgentItemResult.PriceWithVat = campaignPrice.CampaignPriceWithVAT;
            }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.