We have a product field template that always requires custom logic to calculate the price of the variant. The price is calculated according to the product’s relation to other products in the system, so the list price of the product is a “base price” that can be changed when purchasing the product. There is no list price set in Litium for these products since that price is dependent on the price of other products in Litium.
The current implementation overrides the ProductItemViewModelBuilder and uses a custom product page to display the correct price in product listings and on product pages, so the correct calculated price is always shown to users.
However, the current implementation does not work with price agents and price filters, and possibly other scenarios. So we are looking for a way to actually change the price calculation for these products, not just change the price presentation.
Ideally, we would like to replace the price calculation in Litium for a specific product field template, so that our custom price logic is always used instead of getting the price from the price list.
All data needed to calculate the variant price is present inside Litium.
Hi,
Best aproach is to decorate the price calclulator. There is an example about it in the forum article that you have mentioned (Custom PriceCalculator implementation). All you have to do check the PriceCalculatorItemArgs and for the variants belonging to a base product having your specific product field template calculate the price in your way.
Note: Price calculator is used frequently and can cause performance issues if it contains heavy calculations. Do not refer to shopping cart from your custom price caclulator. It causes infinite loops.
Since the custom price logic should only apply to specific product field templates, I want to determine as quickly as possible the product field template of the variant being calculated. Is there a quicker approach than the below?
public IDictionary<Guid, PriceCalculatorResult> GetListPrices(
PriceCalculatorArgs priceCalculatorArgs,
params PriceCalculatorItemArgs[] priceCalculatorItemArgs
)
{
foreach (var item in priceCalculatorItemArgs)
{
var variant = _variantService.Get(item.VariantSystemId);
var product = _baseProductService.Get(variant.BaseProductSystemId);
var template = _fieldTemplateService.GetAll().Single(t =>
t.SystemId == product.FieldTemplateSystemId);
// custom price logic depending on template ID here
@utku and @steve.redstorm, in which scenarios does PriceCalculatorItemArgs[] actually contain more than one item argument? In our development and testing we’ve only ever seen one item argument, so our custom price logic currently only supports one item argument. We’re wondering if this shortcut will cover all scenarios.