ShippingCalculator

ShippingCalculator

ShippingCalculator

The ShippingCalculator is used by a ShippingMethod to calculate the price of shipping on a given Order.

Example

const flatRateCalculator = new ShippingCalculator({
    code: 'flat-rate-calculator',
    description: [{ languageCode: LanguageCode.en, value: 'Default Flat-Rate Shipping Calculator' }],
    args: {
        rate: { type: 'int', config: { inputType: 'money' } },
    },
    calculate: (order, args) => {
        return {
            price: args.rate,
            priceWithTax: args.rate * ((100 + args.taxRate) / 100),
        };
    },
});

Signature

class ShippingCalculator<T extends ShippingCalculatorArgs = {}> implements ConfigurableOperationDef {
  constructor(config: {
        args: T;
        calculate: CalculateShippingFn<T>;
        code: string;
        description: LocalizedStringArray;
    })
}

Members

constructor

method
type:
(config: { args: T; calculate: CalculateShippingFn<T>; code: string; description: LocalizedStringArray; }) => ShippingCalculator

ShippingCalculationResult

The return value of the CalculateShippingFn.

Signature

interface ShippingCalculationResult {
  price: number;
  priceWithTax: number;
  metadata?: Record<string, any>;
}

Members

price

property
type:
number

priceWithTax

property
type:
number
The shipping price including taxes.

metadata

property
type:
Record<string, any>
Arbitrary metadata may be returned from the calculation function. This can be used e.g. to return data on estimated delivery times or any other data which may be needed in the storefront application when listing eligible shipping methods.

CalculateShippingFn

A function which implements the specific shipping calculation logic. It takes an Order and an arguments object and should return the shipping price as an integer in cents.

Should return a ShippingCalculationResult object.

Signature

type CalculateShippingFn<T extends ShippingCalculatorArgs> = (
    order: Order,
    args: ConfigArgValues<T>,
) => ShippingCalculationResult | Promise<ShippingCalculationResult>