Skip to main content

Promotion Action

PromotionAction

An abstract class which is extended by PromotionItemAction, PromotionOrderAction, and PromotionShippingAction.

Signature
class PromotionAction<T extends ConfigArgs = ConfigArgs, U extends PromotionCondition[] | undefined = any> extends ConfigurableOperationDef<T> {
readonly priorityValue: number;
constructor(config: PromotionActionConfig<T, U>)
}

priorityValue

property
number
default:
0

Used to determine the order of application of multiple Promotions on the same Order. See the Promotion priorityScore field for more information.

constructor

method
(config: PromotionActionConfig<T, U>) => PromotionAction

PromotionItemAction

Represents a PromotionAction which applies to individual OrderLines.

Example

// Applies a percentage discount to each OrderLine
const itemPercentageDiscount = new PromotionItemAction({
code: 'item_percentage_discount',
args: { discount: 'percentage' },
execute(ctx, orderLine, args) {
return -orderLine.unitPrice * (args.discount / 100);
},
description: 'Discount every item by { discount }%',
});
Signature
class PromotionItemAction<T extends ConfigArgs = ConfigArgs, U extends Array<PromotionCondition<any>> = []> extends PromotionAction<T, U> {
constructor(config: PromotionItemActionConfig<T, U>)
}

constructor

method
(config: PromotionItemActionConfig<T, U>) => PromotionItemAction

PromotionOrderAction

Represents a PromotionAction which applies to the Order as a whole.

Example

// Applies a percentage discount to the entire Order
const orderPercentageDiscount = new PromotionOrderAction({
code: 'order_percentage_discount',
args: { discount: 'percentage' },
execute(ctx, order, args) {
return -order.subTotal * (args.discount / 100);
},
description: 'Discount order by { discount }%',
});
Signature
class PromotionOrderAction<T extends ConfigArgs = ConfigArgs, U extends PromotionCondition[] = []> extends PromotionAction<T, U> {
constructor(config: PromotionOrderActionConfig<T, U>)
}

constructor

method
(config: PromotionOrderActionConfig<T, U>) => PromotionOrderAction

PromotionShippingAction

Represents a PromotionAction which applies to the shipping cost of an Order.

Signature
class PromotionShippingAction<T extends ConfigArgs = ConfigArgs, U extends PromotionCondition[] = []> extends PromotionAction<T, U> {
constructor(config: PromotionShippingActionConfig<T, U>)
}

constructor

method
(config: PromotionShippingActionConfig<T, U>) => PromotionShippingAction

ExecutePromotionItemActionFn

The function which is used by a PromotionItemAction to calculate the discount on the OrderLine for each item.

Signature
type ExecutePromotionItemActionFn<T extends ConfigArgs, U extends Array<PromotionCondition<any>>> = (
ctx: RequestContext,
orderLine: OrderLine,
args: ConfigArgValues<T>,
state: ConditionState<U>,
promotion: Promotion,
) => number | Promise<number>

ExecutePromotionLineActionFn

The function which is used by a PromotionLineAction to calculate the discount on the OrderLine.

Signature
type ExecutePromotionLineActionFn<T extends ConfigArgs, U extends Array<PromotionCondition<any>>> = (
ctx: RequestContext,
orderLine: OrderLine,
args: ConfigArgValues<T>,
state: ConditionState<U>,
promotion: Promotion,
) => number | Promise<number>

ExecutePromotionOrderActionFn

The function which is used by a PromotionOrderAction to calculate the discount on the Order.

Signature
type ExecutePromotionOrderActionFn<T extends ConfigArgs, U extends Array<PromotionCondition<any>>> = (
ctx: RequestContext,
order: Order,
args: ConfigArgValues<T>,
state: ConditionState<U>,
promotion: Promotion,
) => number | Promise<number>

ExecutePromotionShippingActionFn

The function which is used by a PromotionOrderAction to calculate the discount on the Order.

Signature
type ExecutePromotionShippingActionFn<T extends ConfigArgs, U extends Array<PromotionCondition<any>>> = (
ctx: RequestContext,
shippingLine: ShippingLine,
order: Order,
args: ConfigArgValues<T>,
state: ConditionState<U>,
promotion: Promotion,
) => number | Promise<number>

PromotionActionSideEffectFn

The signature of a PromotionAction's side-effect functions onActivate and onDeactivate.

Signature
type PromotionActionSideEffectFn<T extends ConfigArgs> = (
ctx: RequestContext,
order: Order,
args: ConfigArgValues<T>,
promotion: Promotion,
) => void | Promise<void>

PromotionActionConfig

Configuration for all types of PromotionAction.

Signature
interface PromotionActionConfig<T extends ConfigArgs, U extends Array<PromotionCondition<any>> | undefined> extends ConfigurableOperationDefOptions<T> {
priorityValue?: number;
conditions?: U extends undefined ? undefined : ConditionTuple<Exclude<U, undefined>>;
onActivate?: PromotionActionSideEffectFn<T>;
onDeactivate?: PromotionActionSideEffectFn<T>;
}

priorityValue

property
number
default:
0

Used to determine the order of application of multiple Promotions on the same Order. See the Promotion priorityScore field for more information.

conditions

property
U extends undefined ? undefined : ConditionTuple<Exclude<U, undefined>>

Allows PromotionActions to define one or more PromotionConditions as dependencies. Having a PromotionCondition as a dependency has the following consequences:

  1. A Promotion using this PromotionAction is only valid if it also contains all PromotionConditions on which it depends.
  2. The execute() function will receive a statically-typed state argument which will contain the return values of the PromotionConditions' check() function.

onActivate

property
v1.8.0
experimental

An optional side effect function which is invoked when the promotion becomes active. It can be used for things like adding a free gift to the order or other side effects that are unrelated to price calculations.

If used, make sure to use the corresponding onDeactivate function to clean up or reverse any side effects as needed.

onDeactivate

property
v1.8.0
experimental

Used to reverse or clean up any side effects executed as part of the onActivate function.

PromotionItemActionConfig

Configuration for a PromotionItemAction

Signature
interface PromotionItemActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionItemActionFn<T, U>;
}

execute

The function which contains the promotion calculation logic. Should resolve to a number which represents the amount by which to discount the OrderLine, i.e. the number should be negative.

PromotionLineActionConfig

Configuration for a PromotionLineAction

Signature
interface PromotionLineActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionLineActionFn<T, U>;
}

execute

The function which contains the promotion calculation logic. Should resolve to a number which represents the amount by which to discount the OrderLine, i.e. the number should be negative.

PromotionOrderActionConfig

Signature
interface PromotionOrderActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionOrderActionFn<T, U>;
}

execute

The function which contains the promotion calculation logic. Should resolve to a number which represents the amount by which to discount the Order, i.e. the number should be negative.

PromotionShippingActionConfig

Signature
interface PromotionShippingActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionShippingActionFn<T, U>;
}

execute

The function which contains the promotion calculation logic. Should resolve to a number which represents the amount by which to discount the Shipping, i.e. the number should be negative.

PromotionLineAction

Represents a PromotionAction which applies to individual OrderLines. The difference from PromotionItemAction is that it applies regardless of the Quantity of the OrderLine.

Example

// Applies a percentage discount to each OrderLine
const linePercentageDiscount = new PromotionLineAction({
code: 'line_percentage_discount',
args: { discount: 'percentage' },
execute(ctx, orderLine, args) {
return -orderLine.linePrice * (args.discount / 100);
},
description: 'Discount every line by { discount }%',
});
Signature
class PromotionLineAction<T extends ConfigArgs = ConfigArgs, U extends Array<PromotionCondition<any>> = []> extends PromotionAction<T, U> {
constructor(config: PromotionLineActionConfig<T, U>)
}

constructor

method
(config: PromotionLineActionConfig<T, U>) => PromotionLineAction