Promotion Action
PromotionAction
An abstract class which is extended by PromotionItemAction, PromotionOrderAction, and PromotionShippingAction.
class PromotionAction<T extends ConfigArgs = ConfigArgs, U extends PromotionCondition[] | undefined = any> extends ConfigurableOperationDef<T> {
readonly priorityValue: number;
constructor(config: PromotionActionConfig<T, U>)
}
- Extends:
ConfigurableOperationDef<T>
priorityValue
number
0
Used to determine the order of application of multiple Promotions
on the same Order. See the Promotion priorityScore
field for
more information.
constructor
(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, orderItem, orderLine, args) {
return -orderLine.unitPrice * (args.discount / 100);
},
description: 'Discount every item by { discount }%',
});
class PromotionItemAction<T extends ConfigArgs = ConfigArgs, U extends Array<PromotionCondition<any>> = []> extends PromotionAction<T, U> {
constructor(config: PromotionItemActionConfig<T, U>)
}
- Extends:
PromotionAction<T, U>
constructor
(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 }%',
});
class PromotionOrderAction<T extends ConfigArgs = ConfigArgs, U extends PromotionCondition[] = []> extends PromotionAction<T, U> {
constructor(config: PromotionOrderActionConfig<T, U>)
}
- Extends:
PromotionAction<T, U>
constructor
(config: PromotionOrderActionConfig<T, U>) => PromotionOrderAction
PromotionShippingAction
Represents a PromotionAction which applies to the shipping cost of an Order.
class PromotionShippingAction<T extends ConfigArgs = ConfigArgs, U extends PromotionCondition[] = []> extends PromotionAction<T, U> {
constructor(config: PromotionShippingActionConfig<T, U>)
}
- Extends:
PromotionAction<T, U>
constructor
(config: PromotionShippingActionConfig<T, U>) => PromotionShippingAction
ExecutePromotionItemActionFn
The function which is used by a PromotionItemAction to calculate the discount on the OrderLine.
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>
ExecutePromotionOrderActionFn
The function which is used by a PromotionOrderAction to calculate the discount on the Order.
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.
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
.
type PromotionActionSideEffectFn<T extends ConfigArgs> = (
ctx: RequestContext,
order: Order,
args: ConfigArgValues<T>,
promotion: Promotion,
) => void | Promise<void>
PromotionActionConfig
Configuration for all types of PromotionAction.
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>;
}
- Extends:
ConfigurableOperationDefOptions<T>
priorityValue
number
0
Used to determine the order of application of multiple Promotions
on the same Order. See the Promotion priorityScore
field for
more information.
conditions
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:
- A Promotion using this PromotionAction is only valid if it also contains all PromotionConditions on which it depends.
- The
execute()
function will receive a statically-typedstate
argument which will contain the return values of the PromotionConditions'check()
function.
onActivate
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
Used to reverse or clean up any side effects executed as part of the onActivate
function.
PromotionItemActionConfig
Configuration for a PromotionItemAction
interface PromotionItemActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionItemActionFn<T, U>;
}
- Extends:
PromotionActionConfig<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
interface PromotionOrderActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionOrderActionFn<T, U>;
}
- Extends:
PromotionActionConfig<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
interface PromotionShippingActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionShippingActionFn<T, U>;
}
- Extends:
PromotionActionConfig<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.