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.
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;
}
}
}