Promotions
Promotions are a means of offering discounts on an order based on various criteria. A Promotion consists of conditions and actions.
- conditions are the rules which determine whether the Promotion should be applied to the order.
- actions specify exactly how this Promotion should modify the order.
Parts of a Promotion
Constraints
All Promotions can have the following constraints applied to them:
- Date range Using the "starts at" and "ends at" fields, the Promotion can be scheduled to only be active during the given date range.
- Coupon code A Promotion can require a coupon code first be activated using the
applyCouponCodemutation in the Shop API. - Per-customer limit A Promotion coupon may be limited to a given number of uses per Customer.
Conditions
A Promotion may be additionally constrained by one or more conditions. When evaluating whether a Promotion should be applied, each of the defined conditions is checked in turn. If all the conditions evaluate to true, then any defined actions are applied to the order.
Vendure comes with some built-in conditions, but you can also create your own conditions (see section below).
Actions
A promotion action defines exactly how the order discount should be calculated. At least one action must be specified for a valid Promotion.
Vendure comes with some built-in actions, but you can also create your own actions (see section below).
Creating custom conditions
To create a custom condition, you need to define a new PromotionCondition object.
A promotion condition is an example of a configurable operation.
Here is an annotated example of one of the built-in PromotionConditions.
Custom promotion conditions are then passed into the VendureConfig PromotionOptions to make them available when setting up Promotions:
Creating custom actions
There are three kinds of PromotionAction:
PromotionItemActionapplies a discount on theOrderLinelevel, i.e. it would be used for a promotion like "50% off USB cables".PromotionOrderActionapplies a discount on theOrderlevel, i.e. it would be used for a promotion like "5% off the order total".PromotionShippingActionapplies a discount on the shipping, i.e. it would be used for a promotion like "free shipping".
The implementations of each type is similar, with the difference being the arguments passed to the execute().
Here's an example of a simple PromotionOrderAction.
Custom PromotionActions are then passed into the VendureConfig PromotionOptions to make them available when setting up Promotions:
Free gift promotions
Vendure v1.8 introduced a new side effect API to PromotionActions, which allow you to define some additional action to be performed when a Promotion becomes active or inactive.
A primary use-case of this API is to add a free gift to the Order. Here's an example of a plugin which implements a "free gift" action:
Dependency relationships
It is possible to establish dependency relationships between a PromotionAction and one or more PromotionConditions.
For example, if we want to set up a "buy 1, get 1 free" offer, we need to:
- Establish whether the Order contains the particular ProductVariant under offer (done in the PromotionCondition)
- Apply a discount to the qualifying OrderLine (done in the PromotionAction)
In this scenario, we would have to repeat the logic for checking the Order contents in both the PromotionCondition and the PromotionAction. Not only is this duplicated work for the server, it also means that setting up the promotion relies on the same parameters being input into the PromotionCondition and the PromotionAction.
Note the use of PromotionItemAction to get a reference to the OrderLine as opposed to the Order.
Instead, we can say that the PromotionAction depends on the PromotionCondition:
In the above code, we are stating that this PromotionAction depends on the buyXGetYFreeCondition PromotionCondition. Attempting to create a Promotion using the buy1Get1FreeAction without also using the buyXGetYFreeCondition will result in an error.
In turn, the buyXGetYFreeCondition can return a state object with the type { [key: string]: any; } instead of just a true boolean value. This state object is then passed to the PromotionConditions which depend on it, as part of the last argument (state).
Injecting providers
If your PromotionCondition or PromotionAction needs access to the database or other providers, they can be injected by defining an init() function in your PromotionAction or PromotionCondition. See the configurable operation guide for details.