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 readonly priorityValue: number;
constructor(config: PromotionActionConfig<T, U>)
}
Extends
Members
priorityValue
number
0
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 }%',
});
Signature
class PromotionItemAction<T extends ConfigArgs = ConfigArgs, U extends Array<PromotionCondition<any>> = []> extends PromotionAction<T, U> {
constructor(config: PromotionItemActionConfig<T, U>)
}
Extends
- PromotionAction<T, U>
Members
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 }%',
});
Signature
class PromotionOrderAction<T extends ConfigArgs = ConfigArgs, U extends PromotionCondition[] = []> extends PromotionAction<T, U> {
constructor(config: PromotionOrderActionConfig<T, U>)
}
Extends
- PromotionAction<T, U>
Members
constructor
(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>)
}
Extends
- PromotionAction<T, U>
Members
constructor
(config: PromotionShippingActionConfig<T, U>) => PromotionShippingAction
ExecutePromotionItemActionFn
The function which is used by a PromotionItemAction to calculate the discount on the OrderLine.
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>
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>;
}
Extends
Members
priorityValue
number
0
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
onActivate
function.
PromotionItemActionConfig
Configuration for a PromotionItemAction
Signature
interface PromotionItemActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionItemActionFn<T, U>;
}
Extends
- PromotionActionConfig<T, U>
Members
execute
ExecutePromotionItemActionFn<T, U>
PromotionOrderActionConfig
Signature
interface PromotionOrderActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionOrderActionFn<T, U>;
}
Extends
- PromotionActionConfig<T, U>
Members
execute
ExecutePromotionOrderActionFn<T, U>
PromotionShippingActionConfig
Signature
interface PromotionShippingActionConfig<T extends ConfigArgs, U extends PromotionCondition[]> extends PromotionActionConfig<T, U> {
execute: ExecutePromotionShippingActionFn<T, U>;
}
Extends
- PromotionActionConfig<T, U>
Members
execute
ExecutePromotionShippingActionFn<T, U>