For the complete documentation index, see llms.txt. This page is also available as Markdown.

Discounts

Apply automatic product and order discounts at checkout with JavaScript.

A discount function applies automatic discounts to cart lines or the whole order. 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).

The function's title is shown to buyers at checkout as the discount name — pick something merchant-shippable like 10% off accessories.

Operations

Return an array of operations, or { operations: [...] }. Each operation is an object with exactly one of these keys:

Operation
Shape
Effect

Product discounts

{ productDiscountsAdd: { selectionStrategy: 'FIRST', candidates: [...] } }

Discounts individual cart lines

Order discounts

{ orderDiscountsAdd: { selectionStrategy: 'FIRST', candidates: [...] } }

Discounts the order subtotal

Each candidate is:

selectionStrategy decides what happens when several candidates match the same target: FIRST applies the first one, MAXIMUM the largest. Returning an empty array applies no discount. Operations of any other shape are ignored.

How DataJet manages the discount

  • Discount functions appear in the Shopify admin under Discounts as automatic discounts.

  • The function covers the product and order discount classes; discount codes and shipping discounts 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):

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

Last updated