Discounts
Apply automatic product, order and shipping discounts at checkout with JavaScript.
A discount function applies automatic discounts to cart lines, the whole order or delivery options. Your code inspects the cart and returns a list of operations with discount candidates.
var candidates = input.cart.lines.map(function (line) {
return {
targets: [{ cartLine: { id: line.id } }],
value: { percentage: { value: 10 } },
};
});
return {
operations: [
{ productDiscountsAdd: { selectionStrategy: 'FIRST', candidates: candidates } },
],
};The cart lines are in input.cart.lines, each with an id your candidates target. The discount's own settings are in input.discount (discountClasses).
Discount classes
When you add a discount function, pick one or more discount classes. They decide what the discount can reduce and when your code runs:
Products
Individual cart lines
input.target === 'lines'
Order
The order subtotal
input.target === 'lines'
Shipping
Delivery options, e.g. free Express shipping
input.target === 'delivery'
New discount functions default to Products and Order. You can change the classes later with Edit; the family stays fixed.
Shopify runs the code once for Products/Order (the lines run) and once for Shipping (the delivery run). If you select both kinds, the same code runs twice per checkout, so check input.target and return the operations for that run. Each run keeps only its own operations and ignores the rest.
The two runs receive different input. The lines run gets cart lines with their ids. The delivery run gets input.cart.deliveryGroups[].deliveryOptions, each with a handle and title. Depending on the family it can also get the group id, the option's deliveryMethodType and its cost.amount. The Available input preview in the Add new dialog shows both shapes, and the editor's autocomplete narrows input once you check input.target.
Operations
Return an array of operations, or { operations: [...] }. Each operation is an object with exactly one of these keys:
Product discounts
{ productDiscountsAdd: { selectionStrategy: 'FIRST', candidates: [...] } }
Discounts individual cart lines (lines run)
Order discounts
{ orderDiscountsAdd: { selectionStrategy: 'FIRST', candidates: [...] } }
Discounts the order subtotal (lines run)
Shipping discounts
{ deliveryDiscountsAdd: { selectionStrategy: 'ALL', candidates: [...] } }
Discounts delivery options (delivery run)
Each candidate is:
selectionStrategy decides what happens when several candidates match the same target. For product and order discounts, FIRST applies the first one and MAXIMUM the largest. Shipping discounts only support ALL, which applies every matching candidate. Returning an empty array applies no discount. Operations of any other shape are ignored.
A shipping candidate can target a single delivery option ({ deliveryOption: { handle } }) or every option in a delivery group ({ deliveryGroup: { id } }). Group targets need the group id, which only some families include. To make an option free, use { percentage: { value: 100 } }.
How DataJet manages the discount
Discount functions appear in the Shopify admin under Discounts as automatic discounts.
The function covers the classes you pick: product, order and shipping. Discount codes are not part of discount functions.
Enabling in the Functions Console activates the discount immediately (no start/end scheduling); disabling deactivates it.
DataJet discounts combine with other product, order and shipping discounts.
Shopify allows at most 25 active automatic discounts per store — this cap is shared with the discounts the merchant creates outside DataJet.
Examples
10% off products with a tag (use the Product family with the hasProductTags variable):
Free Express shipping for members (use the Tags family with the hasCustomerTags variable set to ["member"], and the Shipping class):
When a cart ships in several parts, each shipment is its own delivery group with its own options. The loop above discounts the Express option in every group and leaves groups without one unchanged. Match options by title, the name set up in the shipping settings. Use the handle only to target the option you matched; don't hardcode it.
Order discount above a threshold from vars (use the General or Cart family; set MIN_ORDER_TOTAL and ORDER_DISCOUNT_PERCENT as code variables):
Notes
Discount functions don't use the Run on steps. They run whenever the cart changes: the lines run for Products/Order, the delivery run for Shipping (once delivery options are known).
All amounts are in the presentment currency (what the buyer pays).
If your code throws, no discount is applied (fail open).
Last updated