What are Shopify Functions, exactly?
Shopify Functions are WebAssembly modules that Shopify executes on its own infrastructure at a defined point in the cart or checkout flow. The app does not host them, and a module does not call back to the app to decide a price.
The contract has three parts: the app declares a GraphQL input query and the function receives exactly those fields; the module exports a run function that returns a list of operations; Shopify applies them to the cart and shows the result.
That shape has consequences a normal backend does not have:
- The function cannot read a clock or a random number. Shopify does not allow nondeterminism, so a price cannot depend on the time of day.
- The function cannot write to stdout or stderr to debug. The only output is the operation list, plus up to 1 kB of logs.
- The function cannot open a database connection. Anything it needs has to arrive through the input query — a metafield, a cart attribute, or a field Shopify already knows.
- The function runs on every cart interaction in the flow it targets, so its cost is paid on the critical path of checkout.
That is why the limits are design constraints: a function that parses a large JSON blob on every cart change pays that cost inside checkout.
Why Rust rather than JavaScript for a Cart Transform?
Rust wins on the two constraints that decide whether the function survives a large cart: binary size and deterministic instruction count. Shopify’s own guidance says it recommends Rust as the most performant language choice and warns that a slower language can fail with large carts.
JavaScript functions are supported, and they are fine for a small discount rule. Two things make them a poorer fit here. A JavaScript runtime compiled to Wasm spends part of the 256 KiB budget on the runtime itself, and its memory use is less predictable under the 10 MiB cap. Rust has no garbage collector, no hidden allocation per string, and a compiler that will tell you when a data structure is bigger than it needs to be.
The WebAssembly guide also explains the guest API: input values are read lazily through imported functions rather than by loading the whole JSON document into memory. Rust’s generated bindings use that API, which is how a function reads a cart’s line items without allocating a copy of the entire input.
Size then becomes a build check: a Rust function that pulls in a JSON parser or a regex crate fails the limit in CI, and the fix is to read only the fields the query asked for.
Cart Transform or a discount function?
Both can change what a customer pays, and they are not interchangeable. A Cart Transform function returns operations that expand, merge or update cart lines, including an explicit line price. A discount function returns a discount with a value, which Shopify applies within its discount classes and combination rules.
| Question | Cart Transform function | Discount function |
|---|---|---|
| What it sets | A line price, title, image, or bundle expansion | An amount or percentage off |
| Unit price after the run | Exactly what the function returns | Base price minus the discount |
| Applies to | Cart line items | Product, order or shipping discounts |
| Selling plans | Expand, merge and update operations are rejected when a selling plan is present | Works within discount rules |
| Plan requirement | lineUpdate operations need Shopify Plus or a development store |
Available more broadly |
| Multiple apps | One function per app per store; all installed apps’ functions run | Discount combination rules decide |
| Good for | Wholesale tiers, bundles, exact contract pricing | Promotions and campaigns |
Which should you choose?
Choose a Cart Transform when the price is a property of the line rather than a promotion: wholesale quantity breaks, customer-specific pricing, or a bundle whose displayed unit price must equal the charged unit price. Choose a discount function when the goal is a promotion that should combine with others, show as a saving, and stop at a defined date. A tiered price that is really “10% off at 250 units” can be a discount; a tiered price that must be exactly 1.10 per unit at 250 and 1.00 at 500 is a transform.
The runtime limits, translated into design decisions
The published limits are short, and each one removes a design option.
| Limit | What it rules out |
|---|---|
| 256 KiB compiled binary | Bundling a JSON parser, a template engine or a large regex set |
| 10 MiB linear memory | Building an index of the whole catalog at runtime |
| 11 million instructions | Sorting large collections or doing string-heavy matching per line |
| 40 kB output | Rewriting prices for a very large cart in one pass; the docs point to discount functions or targeted products instead |
| 128 kB input | Asking for every field on every object; request only what the calculation needs |
| 3,000-byte input query | Deep nested queries with many aliased fields |
| Input query cost of 30 | Reading many metafields: each metafield field costs 3, so roughly ten fit in the budget |
| Metafields over 10,000 bytes are not returned | Storing a whole price matrix in one metafield value |
The metafield rules catch people. A price-break table stored as JSON in one metafield works while it is small, then silently stops being returned past 10,000 bytes, and every line falls back to the base tier. Splitting the tiers across several metafields helps until the query cost of 30 becomes the limit. The better answer for a large matrix is to keep the authoritative tiers in the app’s database and push only the relevant tier data onto the product, or to store it where the function can read it cheaply.
Building and shipping the function
The build has a fixed sequence, and the interesting parts are the checks between the steps.
- Define the input query in the function’s configuration, requesting only the cart line fields, quantities and the one metafield that holds the tier data.
- Write the run logic so it is a pure function of that input: quantity in, tier price out, no clock, no random, no network.
- Compile to
wasm32and check the artifact size against 256 KiB in CI. Fail the build, not the deploy. - Run the function locally against a saved cart input with the Shopify CLI, including the awkward carts: one line at the tier boundary, a line below the first tier, a cart with a selling plan, and a cart at the 200-line scale where the instruction budget starts to matter.
- Release a version and deploy it with the app.
- Attach it to the store with the
cartTransformCreatemutation, referencing the function id. - Test on a development store, then a Plus store, because
lineUpdateoperations are not available on a standard plan.
Step 4 pays for itself: a tier boundary is an off-by-one waiting to happen, and a selling-plan line is the case where Shopify rejects the operation instead of applying it.
Where the tier data lives
The tier table is small: a few quantity thresholds and a price per unit, per product or per collection. The natural place for it is a metafield on the product, which keeps the data next to the thing it prices and avoids a network call the runtime cannot make anyway.
That choice has a maintenance cost. The metafield has to be written by the app whenever the tier table changes, and the write has to be idempotent, because a partial write means the store charges the wrong tier until the next sync. The same pattern as any other projection: the database is the source, the metafield is a cache, and a mismatch is a bug to detect rather than a state to live with.
What broke and what we changed
The theme showed a different price from the one the cart charged. On a promotional-products store, quantity price breaks lived in a theme snippet of roughly 560 lines of inline JavaScript that read a metafield and rendered a tier table. The product’s headline price came from a product-price web component that renders the variant’s base price and replaces its own HTML on every variant update. The table showed the correct per-unit price for the chosen quantity while the headline price kept showing the base price, so two numbers on the same page disagreed.
Fixing the display did not fix the charge. The proposed theme fix was to re-apply the tier price after the variant update, with a short delay to run after the web component’s own handler. That is a race with someone else’s render loop, and it only ever changes what the shopper sees. Moving the calculation into the cart is what makes the charged price match the displayed one, because the same rule now produces both.
The display code was the specification. The tier snippet already contained the lookup logic — find the tier where quantity is at least the minimum and at most the maximum, treat a missing maximum as unbounded, fall back to the base price below the first tier. That logic became the function’s tests, which is the useful part of finding pricing in a theme: the rules are usually right, and the place they run is wrong.
A selling plan silently disables the transform. Shopify rejects expand, merge and update operations when a selling plan is present. A subscription or pre-order line in the same cart therefore cannot be priced by this function, and the cart has to handle it explicitly rather than assuming every line was transformed.
Binary size creeps in through convenience. The first version of a function tends to pull in a JSON library for a metafield that is really three numbers. A size gate in CI keeps that from becoming a deploy failure later.
Related
Frequently asked questions
What is a Shopify Function?
A Shopify Function is a WebAssembly module that Shopify runs on its own infrastructure during cart or checkout flows. The app supplies a GraphQL input query and the compiled module; Shopify passes the requested fields in, receives a list of operations, and applies them. The app does not host or call the function at runtime.
Why is Rust recommended for Shopify Functions?
Because the runtime is small and deterministic, and Rust produces small binaries with predictable memory use. Shopify's documentation recommends Rust as the most performant choice and warns that slower options can fail on large carts. The 256 KiB binary and 10 MiB memory limits reward a language with no runtime and no garbage collector.
Can a Shopify Function call my server or a database?
No. A function cannot open a network connection or query a database; the only data it sees is what the input query requests. Network access exists as a limited fetch target for some APIs, but the default design is to pre-populate data in metafields, cart attributes, or fields Shopify already exposes.
How many metafields can a function read?
Fewer than you expect. Each metafield field in the input query costs 3 against a total query cost budget of 30, so roughly ten metafields fit, and any metafield value over 10,000 bytes is not returned at all. Large price matrices have to be split or projected down to what the calculation needs.
What is the difference between a Cart Transform and a discount function?
A Cart Transform sets the line price directly, so the charged unit price is exactly what the function returns. A discount function returns an amount or percentage off, which Shopify applies within its discount classes and combination rules. Use a transform for contract or tier pricing, and a discount for promotions.
Why did the theme show one price and the cart another?
Because theme code renders a display and the cart recalculates independently. In this case the price-break table computed a per-unit tier price while a product-price component re-rendered the variant's base price on every variant change. Any pricing rule that exists only in the theme will eventually disagree with the cart.
Does tiered pricing work with subscription or pre-order lines?
Not through a Cart Transform. Shopify rejects expand, merge and update operations when a selling plan is present, so a cart containing a subscription line cannot have that line repriced by the transform. The cart needs an explicit path for those lines rather than assuming the function handled them.
