Back to Arky

Promotions

Code-based discounts, their conditions and effects, promotion codes, and cart usage

A promotion is a discount rule: who gets it (conditions), what it does (effects), how it combines with others (stacking, priority) and how often it can be used. Its activation is fixed when it is made: { type: "codes" } applies through its promotion codes, which a buyer enters, and { type: "automatic" } applies to every cart while it is active, inside its schedule and under its limit, and its conditions hold, without a code. An automatic promotion has no codes (adding one is refused with 409 PROMOTION_CODE.AUTOMATIC_PROMOTION). A promotion is a discount, not stored value.

Effects

import type { PromotionEffect } from "arky-sdk";

const effects: PromotionEffect[] = [
	{
		type: "item_percentage",
		id: crypto.randomUUID(),
		target: { type: "categories", category_ids: [accessoriesCategoryId] },
		basis_points: 1_000,
	},
	{
		type: "delivery_fixed",
		id: crypto.randomUUID(),
		amounts: [
			{ amount: 500, currency: "eur" },
			{ amount: 1_000, currency: "bam" },
		],
	},
];
  • Effects are item_percentage, item_fixed, order_percentage, order_fixed, delivery_percentage, delivery_fixed and buy_x_get_y. Each has an id the app picks, unique within its promotion.
  • Item effects name a target: products, product variants, booking services, package_offerings (buying a package of those offerings), packages (buying that package), listed_in_packages (everything priced from them), categories, or all eligible items.
  • item_fixed takes its amount off each matching unit (scope: { type: "per_unit" }) or once, split across the matching units (scope: { type: "once_per_order" }). Order effects work once across all lines that take promotions, and delivery effects once across the cart’s deliveries.
  • buy_x_get_y repeats as often as the cart allows: each time buy.quantity matching units and get.quantity reward units are there, the cheapest reward units get its percentage off.
  • Percentages are basis points: 100 is 1%, 10_000 is 100%. Fixed amounts list one amount per currency, each currency once, in integer minor units.
  • Effects apply in three rounds: every applied promotion’s item effects, then their order effects, then their delivery effects. Within a round, promotions go from the highest priority down, and each works on what earlier discounts left, so a line never goes below zero.
  • Only lines priced from the cart’s package take promotions. Offer lines and a package line that switches or amends a member take none, and neither does a renewal order. A promotion on a package or offering reduces the package line, never again the lines it generates.

Conditions

All conditions must hold together: signed_in, customer (verified customers only), package, package_offering, company, company_location, market, sales_channel, minimum_subtotal and item_quantity. A package condition holds for the buyer’s packages that count, not for the package the cart shops in, so money off for a club’s members in the normal shop is a promotion with a package condition, optionally with a customer_limit that caps each buyer’s uses, counted per email. A company condition holds for the company’s own carts and its locations’. minimum_subtotal lists one amount per currency and holds when the lines that take promotions add up to it, at their prices before any discount and without separately added tax. item_quantity names a target, like an item effect, with min_quantity and an optional max_quantity, both included: it holds when the cart’s units matching the target, counted across its lines before any discount, fall in that range. Quantity discounts are a code whose item_quantity condition and item effect name the same goods, such as at least 20 cartons of milk for 15% off those cartons; ranges such as 20 to 49 and 50 and more are two promotions.

All the fixed amounts and the minimum of one promotion list the same currencies. A promotion with fixed amounts or a minimum applies only in carts whose currency they list; a promotion with only percentages applies in any currency.

A condition or target that lists records lists at least one: a create with an empty list, or an update that adds or changes such a condition or effect, is refused with 409 PROMOTION.EMPTY_LIST. Deleting a category or erasing a customer can still empty a list later; an empty list matches nothing, so a promotion never opens up to everyone.

Create, Read, Update, Delete

POST /v1/stores/{store_id}/promotions
SDK: sdk.eshop.promotion.create()
GET /v1/stores/{store_id}/promotions
SDK: sdk.eshop.promotion.find()
PUT /v1/stores/{store_id}/promotions/{id}
SDK: sdk.eshop.promotion.update()
DELETE /v1/stores/{store_id}/promotions/{id}?expected_updated_at=
SDK: sdk.eshop.promotion.delete()
POST /v1/stores/{store_id}/promotion-codes
SDK: sdk.eshop.promotionCode.create()
GET /v1/stores/{store_id}/promotion-codes
SDK: sdk.eshop.promotionCode.find()
GET /v1/stores/{store_id}/promotion-codes/by-code/{code}
SDK: sdk.eshop.promotionCode.getByCode()
import { epochMilliseconds } from "arky-sdk/utils";

const promotion = await sdk.eshop.promotion.create({
	store_id: storeId,
	id: crypto.randomUUID(),
	key: "summer-20",
	blocks: [{ id: crypto.randomUUID(), key: "name", type: "localized_text", value: { en: "Summer sale" } }],
	activation: { type: "codes" },
	conditions: [{ type: "minimum_subtotal", amounts: [{ amount: 5_000, currency: "eur" }] }],
	effects: [{ type: "order_percentage", id: crypto.randomUUID(), basis_points: 2_000 }],
	stacking: { type: "stop_after" },
	priority: 10,
	limit: { type: "limited", max_uses: 1_000 },
	customer_limit: { type: "limited", max_uses: 1 },
	schedule: {
		type: "scheduled",
		starts_at: epochMilliseconds(Date.UTC(2026, 5, 1)),
		ends_at: epochMilliseconds(Date.UTC(2026, 8, 1)),
	},
	status: { type: "active" },
});

const code = await sdk.eshop.promotionCode.create({
	store_id: storeId,
	id: crypto.randomUUID(),
	promotion_id: promotion.id,
	code: "SUMMER20",
	limit: { type: "unlimited" },
	status: { type: "active" },
});

const freeShipping = await sdk.eshop.promotion.create({
	store_id: storeId,
	id: crypto.randomUUID(),
	key: "free-shipping-over-50",
	blocks: [{ id: crypto.randomUUID(), key: "name", type: "localized_text", value: { en: "Free shipping over €50" } }],
	activation: { type: "automatic" },
	conditions: [{ type: "minimum_subtotal", amounts: [{ amount: 5_000, currency: "eur" }] }],
	effects: [{ type: "delivery_percentage", id: crypto.randomUUID(), basis_points: 10_000 }],
	stacking: { type: "continue" },
	priority: 5,
	limit: { type: "unlimited" },
	customer_limit: { type: "unlimited" },
	schedule: { type: "always" },
	status: { type: "active" },
});
  • The key is unique in the store (409 PROMOTION.KEY_TAKEN), and so is the priority (409 PROMOTION.PRIORITY_TAKEN). A code is unique in the store (409 PROMOTION_CODE.CODE_TAKEN).
  • blocks hold what a storefront or an email shows for the promotion, such as its name and terms. Every email that carries an order lists each promotion the order applied with its current blocks at order.promotions, read when the email is made and never stored on the order.
  • schedule is { type: "always" } or { type: "scheduled", starts_at, ends_at }, where ends_at may be null for “from then on”. Codes share their promotion’s dates.
  • limit caps a promotion’s uses in all, and a code’s, as { type: "limited", max_uses } or { type: "unlimited" }. A promotion is eligible while it is active, inside its schedule and under its limit, and its conditions hold; a promotion with codes also needs the cart to carry one of its active codes under that code’s own limit. An eligible promotion applies only when one of its effects takes something off this cart.
  • customer_limit caps a promotion’s uses per email in the same shape. A limited one counts a signed-in customer by their verified email and a guest by the checkout’s contact email, so a guest checkout using such a code needs one. An automatic promotion with a limited customer_limit applies only when the checkout carries an email still under it, and never refuses a checkout.
  • A promotion or code with a limited limit takes its use when checkout is accepted, in the same write as the order, so its cap is never passed. One with an unlimited limit keeps no counter on its record: its uses are counted a moment after checkout and read back as its uses. Whether a promotion or a code has a cap is fixed when it is made (409 PROMOTION.MAX_USES_FIXED); an edit may change a limited cap’s max_uses. An edit may change customer_limit either way. A stopped promotion, or one that takes nothing off, takes no use. An order the buyer never pays gives its uses back once all its lines end expired or payment failed; any other cancellation gives nothing back. A use changes only uses, never the promotion’s or the code’s updated_at, so an edit loaded before it still saves.
  • A store holds at most 100 active automatic promotions (409 PROMOTION.AUTOMATIC_LIMIT), and switching one on is refused when the store’s active automatic promotions together would pass what one quote evaluates (409 PROMOTION.EVALUATION_LIMIT), because a shopper can’t take an automatic promotion off a cart.
  • An update sends the expected_updated_at it read and the complete conditions and effects.
  • A code can be deleted while only carts name it; deleting takes it off those carts. A promotion can be deleted while only its own codes name it; a promotion an order names is archived instead.
  • Webhooks post promotion.created, promotion.updated, promotion.deleted, promotion_code.created and promotion_code.deleted. Uses taken or given back don’t post promotion.updated.

In The Cart

The storefront sets the codes on the cart and reads the discount from the quote:

const cart = arky.eshop.cart({ buyer: { type: "customer" }, package_id: shopPackage.id });
const kept = cart.promotion_codes.get().map((entry) => entry.code);
await cart.setPromotionCodes({ promotion_codes: [...kept, "SUMMER20"] });

const quote = await cart.quote({ contact_email: "[email protected]" });
const notApplied = quote.promotion_codes.filter((entry) => entry.state !== "applied");
console.log(quote.totals.discount, quote.promotions, notApplied);
  • Codes and automatic promotions sit in one order by their unique priority. stacking says what a promotion does to those below it: continue lets them apply, and a stop_after promotion that takes money off stops every promotion below it, their item, order and delivery effects alike; one that takes nothing off stops none. Checkout never compares discounts: the order decides.
  • A cart may hold several codes, and nothing is refused for stacking. setPromotionCodes replaces the whole list, so pass the codes you keep plus the new one. The quote lists every code the cart holds in promotion_codes, each with its state: applied, stopped (a promotion above it stopped it) or not_applied (its conditions don’t hold, it takes nothing off this cart, or another code of the same promotion applied first). Show “code not applied” for the last two: they spend no use and stay in the cart, applying as soon as the cart lets them. The cart store’s promotion_codes atom holds the same list.
  • A code whose promotion has a limited customer_limit counts a guest by the contact email, so a guest’s quote sends contact_email once it is known; without it such a code’s quote is refused with 409 CART.PROMOTION_EMAIL_REQUIRED. The checkout flow sends it after setContact.
  • Storefront code never computes its own discounted total. The quote and checkout check each promotion against the cart, and the order keeps each promotion it applied with its key and its activation: the code it applied through, or automatic.

To show a promotion’s name or terms on the storefront, read its blocks by id:

GET /v1/storefront/promotions/{id}
SDK: arky.eshop.promotion.get()
const shown = await arky.eshop.promotion.get({ id: quote.promotions[0].promotion_id });

The storefront reads an active or archived promotion’s id, key and blocks only, never its conditions or counters.