case study · functions · checkout · metafields
Free shipping the checkout actually honors
The problem with promise bars. Every theme can draw a “you’ve unlocked free shipping!” progress bar. Most of them are decoration: the bar and the actual shipping charge are configured in two different places, and the day someone edits one without the other, the cart promises what checkout refuses.
One config, both surfaces. On this demo store the threshold lives in a
single shop metafield, custom.cart_promo_config. The theme’s progress bar
reads it in Liquid. And a Shopify Function — running on Shopify’s checkout
infrastructure, via the unified Discount Function API — reads the same
metafield through its input query and applies a 100% delivery discount when
the cart’s pre-discount subtotal crosses the threshold:
query Input {
cart {
lines { cost { subtotalAmount { amount } } }
deliveryGroups { id }
}
shop { metafield(namespace: "custom", key: "cart_promo_config") { value } }
discount { discountClasses }
}
The decision logic is a pure function with unit tests for every fallback: missing config, unparseable JSON, unknown config version, disabled flag, sub-threshold cart — all of them fail closed (normal shipping rates, no error), matching the theme’s “hide rather than lie” rule.
Discount stacking is a design decision, not luck. The store also runs a
20% automatic product discount. Shopify only combines discounts whose
combinesWith rules allow it — so activation explicitly marks the product
discount as combinable with shipping discounts. Without that line, checkout
would silently apply one and drop the other; a detail that regularly bites
production stores.
Why Functions matter. Checkout scripts were deprecated; Functions — JavaScript or Rust compiled to WebAssembly, deployed as app extensions — are how Shopify Plus-grade checkout logic is built now. This one is deliberately small, but it exercises the full toolchain: input queries against typed schemas, WASM builds, versioned deploys, and activation through the Admin API.
Try it. Add candles until the cart bar says free shipping is unlocked (over $80), then head to checkout — test mode, no real payment. The shipping line reads $0.00 with “Free shipping unlocked”. Drop below the threshold and the discount disappears with the bar.