Custom price logic for specific product field template

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.

I’ve seen this Custom PriceCalculator implementation? - Questions - Litium Forum but I don’t understand it and don’t know if it’s still relevant. I’ve also seen Price lists and calculations (litium.com) but I’m not sure the context is applicable.

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.

What would be the best approach to this?

Litium version: 7.6.2

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.

1 Like

Thank you.

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

No need to get all templates and single out the one you want, get the template like this instead:

var template = _fieldTemplateService.Get<ProductFieldTemplate>(product.FieldTemplateSystemId);
1 Like

Thank you.

@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.

I think it will cover all the scenarios. It is not used with more than one item in the Accelerator.

1 Like

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