Price calculation

Advanced license required

Features described on this page require the Xperience by Kentico Advanced license tier.

The price calculation service IPriceCalculationService calculates prices, promotions, taxes, shipping costs, and totals across the entire commerce experience – from product catalog displays through shopping cart summaries to final checkout totals.

Different calculation modes optimize the service for each scenario, running only the steps required for that context. This page explains the calculation pipeline and how to choose the right mode for your use case.

Key points

  • The price calculation service uses a pipeline architecture with sequential calculation steps.
  • Three calculation modes (Catalog, ShoppingCart, Checkout) optimize performance for different scenarios.
  • You must implement a product data retriever to connect the service to your catalog. See Price calculation implementation.
  • Tax calculation requires a custom implementation – the default step does not calculate taxes. See Price calculation implementation.
  • All calculation steps can be customized or extended to fit your business logic.

Minimum required implementation

To make the price calculation API usable in your project, you need to implement two components:

Component

Required for

Description

Product data retriever

All modes

Implements IProductDataRetriever to load product information and pricing data from your catalog. See Implement product data retrieval.

Tax calculation step

ShoppingCart, Checkout modes

Overrides the default ITaxPriceCalculationStep to calculate taxes. The default implementation does not add taxes. See Price calculation implementation.

Once you’ve implemented and registered these components, the price calculation service automatically handles the rest of the calculation pipeline, including unit price calculations, catalog promotions, subtotals, order promotions, shipping costs, and final totals.

For complete implementation examples, examine the ProductDataRetriever.cs and DancingGoatTaxPriceCalculationStep.cs files in the Dancing Goat sample project.

Calculation modes

The price calculation service supports three calculation modes that determine which steps execute. Set the Mode property on the PriceCalculationRequest to control the calculation scope.

Mode

Use case

Steps executed

Catalog

Product listings, category pages, product detail pages

Product data loading, catalog promotions, line subtotals

ShoppingCart

Shopping cart display before shipping selection

All Catalog steps plus order promotions, taxes, line totals, order totals

Checkout

Final checkout with shipping

All steps including shipping calculation

The default mode is PriceCalculationMode.Catalog. Always set the appropriate mode based on your context to ensure correct calculations and optimal performance.

C#
Set calculation mode based on context

var catalogRequest = new PriceCalculationRequest
{
    Items = productItems,
    LanguageName = "en",
    // Defaults to 'Catalog' if not set
    Mode = PriceCalculationMode.Catalog
};

For detailed usage examples of each mode, see Calculate prices.

Price calculation flow

The price calculation service transforms a PriceCalculationRequest into a PriceCalculationResult through a series of sequential steps:

  1. Input preparation – You provide a PriceCalculationRequest containing items to be priced with their product identifiers and quantities. Depending on the calculation mode, you may also include customer details, selected shipping and payment methods, and delivery addresses.

  2. Price calculation steps – The IPriceCalculationService executes a series of calculation steps in sequence. Each step performs a specific pricing task, modifying the PriceCalculationResult as it progresses through the pipeline.

  3. Result compilation – The service returns a PriceCalculationResult containing itemized pricing details, subtotals, shipping costs, taxes, and the grand total.

Price calculation flow in Xperience

Calculation steps

The calculation pipeline consists of the following steps. Each step only runs if included in the selected calculation mode.

Step

Modes

Description

Load product data

All

Loads product information and pricing data using your IProductDataRetriever implementation

Catalog promotions

All

Applies catalog promotions to individual products (best discount wins)

Line subtotals

All

Calculates line subtotals after catalog discounts

Shipping

Checkout

Determines shipping cost based on the selected shipping method

Order promotions

ShoppingCart, Checkout

Applies order promotions that discount the entire order

Taxes

ShoppingCart, Checkout

Calculates taxes (requires custom implementation)

Line totals

ShoppingCart, Checkout

Calculates final line totals including taxes

Order total

ShoppingCart, Checkout

Sums line subtotals, shipping, and taxes

Grand total

ShoppingCart, Checkout

Sets the final grand total to be paid

You can customize the default flow by modifying existing steps or implementing custom steps and inserting them into the pipeline.

Basic usage

Inject IPriceCalculationService and call Calculate with a PriceCalculationRequest. The service returns a PriceCalculationResult containing itemized prices, totals, and applied promotions.

C#
Calculate prices for cart items

public class ShoppingCartController : Controller
{
    private readonly IPriceCalculationService priceCalculationService;

    public ShoppingCartController(IPriceCalculationService priceCalculationService)
    {
        this.priceCalculationService = priceCalculationService;
    }

    public async Task<IActionResult> DisplayCart(IEnumerable<ShoppingCartItem> cartItems)
    {
        // Build the request with items to price
        var request = new PriceCalculationRequest
        {
            Items = cartItems.Select(item => new PriceCalculationRequestItem
            {
                ProductIdentifier = item.ProductSKU,
                Quantity = item.Quantity
            }).ToList(),
            LanguageName = "en",
            Mode = PriceCalculationMode.ShoppingCart
        };

        // Calculate prices
        PriceCalculationResult result = await priceCalculationService.Calculate(request);

        // Use the results
        var viewModel = new CartViewModel
        {
            Items = result.Items,
            Subtotal = result.TotalPrice,
            Tax = result.TotalTax,
            Total = result.GrandTotal
        };

        return View(viewModel);
    }
}

For complete examples of each calculation mode, see Calculate prices.

Next steps