Umbraco Commerce
CMSCloudHeartcoreDXP
10.latest (LTS)
10.latest (LTS)
  • Umbraco Commerce Documentation
  • Release Notes
  • Commerce Products
    • Commerce Packages
    • Commerce Payment Providers
    • Commerce Shipping Providers
  • Installation
    • Installing Umbraco Commerce
    • Licensing
  • Upgrading
    • Upgrading Umbraco Commerce
    • Version Specific Upgrade Notes
    • Migrate from Vendr to Umbraco Commerce
      • Migrate Umbraco Commerce Checkout
      • Migrate custom Payment Providers
  • Getting Started
    • Introduction
    • Umbraco Configuration
    • User Interface
  • How-To Guides
    • Overview
    • Configure SQLite support
    • Limit Order Line Quantity
    • Use an Alternative Database for Umbraco Commerce Tables
  • Key Concepts
    • Get to know the main features
    • Base Currency
    • Bulk Actions
    • Calculators
    • Dependency Injection
    • Discount Rules / Rewards
    • Events
    • Fluent API
    • Order Calculation State
    • Payment Forms
    • Payment Providers
    • Pipelines
    • Price/Amount Adjustments
    • Price Freezing
    • Product Adapters
    • Product Bundles
    • Product Variants
      • Complex Variants
    • Properties
    • ReadOnly and Writable Entities
    • Search Specifications
    • Settings Objects
    • Tax Sources
    • UI Config Files
    • Umbraco Properties
    • Unit of Work
    • Umbraco Commerce Builder
  • Tutorials
    • Overview
  • Reference
    • Go behind the scenes
Powered by GitBook
On this page
  • Defining a Custom Calculator Implementation
  • Registering a custom Calculator implementation

Was this helpful?

Edit on GitHub
Export as PDF
  1. Key Concepts

Calculators

Performing calculations with Calculators in Umbraco Commerce.

Calculators are small service implementations with the sole responsibility of calculating prices for a given aspect of an Order. There are five main Calculator service interfaces in Umbraco Commerce:

  • IShippingCalculator - Responsible for calculating the Shipping Method price/tax rate of a given Shipping Method.

  • IPaymentCalculator - Responsible for calculating the Payment Method price/tax rate of a given Payment Method.

  • IProductCalculator - Responsible for calculating the Product unit price/tax rate of a given Product.

  • IOrderLineCalculator - Responsible for calculating the price/tax rate of a given OrderLine.

  • IOrderCalculator - Responsible for calculating the entire Order.

All Calculator services can be replaced with alternative implementations should you wish to change how Umbraco Commerce performs its calculations.

Defining a Custom Calculator Implementation

The individual Calculator interfaces may differ but the process for defining a custom Calculator implementation is the same for all of them. It is possible to create a new class that implements the default system Calculator that you wish to replace. You can then override the relevant calculation methods.

public class MyProductCalculator : ProductCalculator
{
    public MyProductCalculator(ITaxService taxService, IStoreService storeService)
        : base(taxService, storeService)
    { }

    public override TaxRate CalculateProductTaxRate(IProductSnapshot productSnapshot, TaxSource taxSource, TaxRate fallbackTaxRate)
    {
        // Do custom tax rate calculation here
    }

    public override Price CalculateProductPrice(IProductSnapshot productSnapshot, Guid currencyId, TaxRate taxRate)
    {
        // Do custom price calculation here
    }
}

Registering a custom Calculator implementation

public static class UmbracoCommerceUmbracoBuilderExtensions
{
    public static IUmbracoCommerceBuilder AddMyServices(IUmbracoCommerceBuilder builder)
    {
        // Replacing the product calculator implementation
        builder.Services.AddUnique<IProductCalculator, MyProductCalculator>();

        // Return the builder to continue the chain
        return builder;
    }
}
PreviousBulk ActionsNextDependency Injection

Last updated 1 year ago

Was this helpful?

Calculators are interface using the AddUnique<TServiceInterface, TReplacementService>() method on the Services property. The TServiceInterface parameter in this case is the Calculator interface Type you wish to replace and TReplacementService is the Type of your custom Calculator implementation.

registered via the IUmbracoCommerceBuilder