# Campaign prices stop working then adding custom IIndexingProviderPreProcessor

**URL:** <https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469>\
**Category:** Questions\
**Created:** [January 9, 2019, 12:15pm UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469 "2019-01-09T12:15:53Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [January 9, 2019, 12:15pm UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/1 "2019-01-09T12:15:53Z")

</div>

I’m trying to implement an IIndexingProviderPreProcessor that should (amongst other things) load and index campaign prices for variants. But as soon as I inject an ICampaignPriceCalculator in the constructor (or resolve it through IoC.Resolve), the campaign price calculation seems to stop working and no campaign prices are shown on the site anymore. As soon as I remove the ICampaignPriceCalculator from my class, the campaign prices start working again.

I’m able to recreate the behavior in Accelerator v6.2.1.

Is this a bug or am I doing it the wrong way? Should I be using something else instead of ICampaignPriceCalculator to get campaign prices?

Litium version: 6.2.1, 6.2.2

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [January 10, 2019, 8:06am UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/2 "2019-01-10T08:06:13Z")

</div>

If you remove the blackhold-filter for the logging `<logger name="Litium.Foundation.Search.Providers.IndexingProvider+IndexPreProcessor" minlevel="Trace" writeTo="BlackHole" final="true" />` will you get any more information out that can help understand why the `IIndexingProviderPreProcessor` not is working?

---

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [January 10, 2019, 9:00am UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/3 "2019-01-10T09:00:45Z")

</div>

No info in the log. My IIndexingProviderPreProcessor is working like a charm in itself, it’s just that as soon as I inject an ICampaignPriceCalculator into it, the method ICampaignPriceCalculator.GetCampaignPrices starts returning null for all products. Regardless of where the method is called from.

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [January 10, 2019, 9:10am UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/4 "2019-01-10T09:10:19Z")

</div>

How do you build the `CampaignPriceCalculatorArgs` in your `IIndexingProviderPreProcessor`?

---

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [January 10, 2019, 9:40am UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/5 "2019-01-10T09:40:31Z")

</div>

As for now I’m not even calling the ICampaignPriceCalculator from my IIndexingProviderPreProcessor, I’m just injecting a dependency to it. But when the calculator is called from other parts of the site (like product details page) the args are built like this:

```
public static decimal GetCampaignPrice(Guid variantId, bool withVat = false)
    {
        var currency = CurrentState.Current.WebSite.Currency;
        var language = CurrentState.Current.WebSite.Language;
        var websiteId = CurrentState.Current.WebSite.ID;
        var token = CurrentState.Current.Token;

        if (currency == null || language == null || websiteId == Guid.Empty)
        {
            return 0M;
        }

        var priceCalculatorArgs = new CampaignPriceCalculatorArgs
        {
            CurrencySystemId = currency.ID,
            DateTimeUtc = DateTime.Now,
            VariantSystemId = variantId,
            Quantity = 1,
            WebSiteID = websiteId,
            UserID = token.UserID
        };

        _campaignPriceCalculator.GetCampaignPrices(priceCalculatorArgs).TryGetValue(priceCalculatorArgs.VariantSystemId, out var listPrice);
        return (listPrice == null) ? 0M : withVat ? listPrice.CampaignPriceWithVAT : listPrice.CampaignPrice;
    }
```

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [January 10, 2019, 9:44am UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/6 "2019-01-10T09:44:41Z")

</div>

The indexing is executed on background threads and have no access to `CurrentState.Current` or `HttpContext.Current`. Also the `CampaignPriceCalculatorArgs` has an argument for `OrderCarrier` that need to be set if you not execute the `ICampaignPriceCalculator` in a context that have access to `HttpContext.Current`.

If you debug your code, where will it throw exception or is it just return `0M` ?

---

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [January 10, 2019, 10:17am UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/7 "2019-01-10T10:17:05Z")

</div>

Good to know!

It returns null. No Exception throwing.

I installed a fresh Accelerator (6.2.2, B2C version) and was able to reproduce the problem by:

1. creating an assortment wide “Reduce product price by percentage” campaign, activating the campaign and verifying that the campaign prices were shown on the site

2. modifying the CampaignIndexingProviderPreProcessor ctor like this:

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [January 10, 2019, 10:22am UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/8 "2019-01-10T10:22:55Z")

</div>

And no exception during startup of the website that says that the `CampaignIndexingProviderPreProcessor` not could be created?

---

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [January 10, 2019, 10:30am UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/9 "2019-01-10T10:30:34Z")

</div>

No exception. Added a breakpoint to the CampaignIndexingProviderPreProcessor to verify that it’s running, and this breakpoint was hit when I updated a product through Backoffice.

---

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [January 10, 2019, 11:07am UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/10 "2019-01-10T11:07:29Z")

</div>

If I move the row `var test = IoC.Resolve<ICampaignPriceCalculator>();` from the constructor to the PreProcessDocument method the problem goes away.

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [January 10, 2019, 12:14pm UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/11 "2019-01-10T12:14:42Z")

</div>

The problem you are getting is that `IIndexingProviderPreProcessor` is initiated before the module _ECommerce_ have been initiated and the implementation of `ICampaignPriceCalculator` is checking if the _ECommerce_ have been initialized when it is created and in this case it says that the **_ECommerce_ does not exists** and **skipping all usage of the campaign engine** , both for indexing and to requests to the public website.

If you build an preprocessor like the following we are initializing the `ICampaignPriceCalculator` at a later stage.

```auto
using System;
using System.Linq;
using Litium.Foundation.Modules.CMS;
using Litium.Foundation.Modules.ECommerce;
using Litium.Foundation.Modules.ECommerce.Carriers;
using Litium.Foundation.Modules.ECommerce.Plugins.PriceCalculator;
using Litium.Foundation.Modules.ProductCatalog.Search;
using Litium.Foundation.Search.Providers;
using Litium.Framework.Search.Indexing;

namespace Litium.Accelerator.Search.Filtering
{
    internal class SetCampaignPriceFromCampaignPriceCalculator : IIndexingProviderPreProcessor
    {
        private ICampaignPriceCalculator _campaignPriceCalculator;

        public IndexDocument PreProcessDocument(IndexDocument document, string indexName)
        {
            if (ProductCatalogSearchDomains.Products.Equals(indexName, StringComparison.OrdinalIgnoreCase))
            {
                var articleIds = document.Tags.Where(x => x.Name.Equals(TagNames.VariantSystemId, StringComparison.OrdinalIgnoreCase)).Select(x => (Guid)x.OriginalValue).ToList();
                foreach (var item in articleIds
                    .Select(x => GetCampaignPrice(x, true)))
                {
                    document.Tags.Add(new ReadableDocumentTag("campaignprice-with-vat-from-campaign-engin", item));
                }
            }
            return document;
        }

        public decimal GetCampaignPrice(Guid variantId, bool withVat = false)
        {
            var campaignPriceCalculator = _campaignPriceCalculator ?? (ModuleECommerce.ExistsInstance ? (_campaignPriceCalculator = IoC.Resolve<ICampaignPriceCalculator>()) : null);
            if (campaignPriceCalculator == null)
            {
                return 0M;
            }

            var currency = CurrentState.Current.WebSite.Currency;
            var language = CurrentState.Current.WebSite.Language;
            var websiteId = CurrentState.Current.WebSite.ID;
            var token = CurrentState.Current.Token;

            if (currency == null || language == null || websiteId == Guid.Empty)
            {
                return 0M;
            }

            var priceCalculatorArgs = new CampaignPriceCalculatorArgs
            {
                CurrencySystemId = currency.ID,
                DateTimeUtc = DateTime.Now,
                VariantSystemId = variantId,
                Quantity = 1,
                WebSiteID = websiteId,
                UserID = token.UserID,
                OrderCarrier = new OrderCarrier()
            };

            return campaignPriceCalculator.GetCampaignPrices(priceCalculatorArgs)
                .TryGetValue(priceCalculatorArgs.VariantSystemId, out var listPrice)
                ? (withVat ? listPrice.CampaignPriceWithVAT : listPrice.CampaignPrice)
                : 0M;
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![malin.oborn](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@malin.oborn](https://forum.litium.com/u/malin.oborn)\
**Post date:** [January 17, 2019, 2:51pm UTC](https://forum.litium.com/t/campaign-prices-stop-working-then-adding-custom-iindexingproviderpreprocessor/469/12 "2019-01-17T14:51:16Z")

</div>

So you can basically break the campaign engine if you inject the ICampaignPriceCalculator in the wrong place? That sounds dangerous, it should be documented in some way. Maybe some kind of flow chart/dependency diagram of Litium’s initialization process?

Your suggested solution works fine. Thanks!
