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',
ui: { component: 'currency-form-input' },
},
taxRate: {
type: 'int',
ui: { component: 'number-form-input', suffix: '%' },
},
},
calculate: (order, args) => {
return {
price: args.rate,
taxRate: args.taxRate,
priceIncludesTax: ctx.channel.pricesIncludeTax,
};
},
});
Signature
class ShippingCalculator<T extends ConfigArgs = ConfigArgs> extends ConfigurableOperationDef<T> {
constructor(config: ShippingCalculatorConfig<T>)
}
Extends
Members
constructor
(config: ShippingCalculatorConfig<T>) => ShippingCalculator
ShippingCalculationResult
The return value of the CalculateShippingFn.
Signature
interface ShippingCalculationResult {
price: number;
priceIncludesTax: boolean;
taxRate: number;
metadata?: Record<string, any>;
}
Members
price
number
priceIncludesTax
boolean
taxRate
number
metadata
Record<string, any>
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 ConfigArgs> = (
ctx: RequestContext,
order: Order,
args: ConfigArgValues<T>,
method: ShippingMethod
) => CalculateShippingFnResult