Shopify volume discount with Shopify Functions – and the merchant edits the tiers in the admin, no deploy: buy 3 save 5%, buy 5 save 10%, buy 10 save 15%. Episode 4 of the Shopify Functions 2027 series (API 2027-01). Real store: 3 tees $72 → $68.40 (5%), 5 tees $120 → $108 (10%); then the merchant changes tier 2 to 20% in the discount settings card, and 6 tees come to $115.20 ($19.20 each). Limited to one collection, 3 serums get 5% while the tees pay full price.
Chapters
- 0:00 Intro: volume tiers the merchant edits
- 0:39 The problem
- 1:04 Three pieces
- 1:41 The metafield
- 2:12 The Function
- 3:02 The input query
- 3:40 The code
- 4:28 The settings card
- 5:38 Real store demo
- 6:01 A real error
- 6:44 Tiers in the cart
- 7:09 The merchant edits it
- 8:08 input_variables in action
- 9:32 Common errors
- 10:04 Recap
Step by step (full walkthrough)
Intro: volume tiers the merchant edits
Six shirts in the cart: 20% off each. And the merchant set that 20% here, in the Shopify admin. Not in my code, and with no deploy.
This is episode four of Shopify Functions 2027: a volume discount the merchant controls. The tiers live in a metafield on the discount, and our Function reads them. A discount settings UI extension lets the merchant edit them.
A collection filter, fed by the new input variables. And a real error I hit on the first save, with the fix.
The problem
In episode three, our tiers were constants in the JavaScript. To change 10% to twenty, a developer edits the code, builds, and deploys a new app version. But the merchant runs the sales.
They want to change the numbers themselves, today, without calling you. So in this episode, the numbers move out of the code and into the Shopify admin.
Three pieces
Here is the whole design, in three pieces. One: a settings card on the discount page. That is an admin UI extension.
When the merchant saves, it writes the settings into a metafield. Two: the metafield itself. It lives on the discount, it belongs to our app, and its value is JSON.
Three: our Function reads that metafield in two ways. The tiers, as a JSON value in the input query. And the collection list, through input variables, which feed the query itself.
Let me show you each one.
The metafield
This is the real value of the metafield on our store. Three tiers. Buy at least three, 5%.
At least five, 10%. At least ten, 15%. And a list of collection IDs.
Empty means the discount applies to every product. It is stored on the discount, in the app reserved namespace, dollar app, with the key function configuration. Keep the value small: a Function does not receive metafield values over 10,000 bytes.
The Function
We generate a new Discount Function, volume tiers, in JavaScript. Again the template writes API version 2026, oh seven, so we set 2027, oh one. Just one target this time: cart lines.
Product discounts only. And here is the new line: input variables, pointing at our metafield. In 2027, oh one, input variables are declared on each targeting block.
The old extensions input variables section is deprecated, and declaring both fails the deploy. And extensions.UI, with the handle of our settings extension. That links the two, so the admin knows which card belongs to this Function.
Then schema, typegen and build, as in episode two.
The input query
The query now takes a variable: collection IDs. Shopify fills it from the metafield. Every top level key in the JSON becomes a variable with the same name.
For every line we ask the quantity, and in any collection, with that variable. So Shopify answers true or false for each product. We never load the collections ourselves.
And on the discount, the same metafield again, as a JSON value. That gives us the tiers. Watch the budget: a metafield field and in any collection cost three points each, of the thirty a query may use.
The code
The guard from episode three: product class only. Read the config. If the merchant never saved settings, we fall back to default tiers.
Then we sort the tiers, biggest first. For every cart line: if the merchant limited the discount to collections, and this product is not in one, we skip it. Then we find the first tier the quantity reaches.
Because the list is sorted, that is the biggest one. One candidate per line, with a message like buy five plus, save 10%. Strategy all, so every line gets its own candidate.
Episode three used maximum to pick one tier for the order. Here each line is different. About forty lines, and no number from the promotion is written in the code.
The settings card
Now the merchant side. shopify app generate extension, template discount details function settings, flavor Preact. The target: admin discount details function settings render.
That is the card inside the discount page. And extensions metafields: the metafield the card wants to read. Shopify hands its current value to the extension when the page opens.
The card is Preact with Shopify's web components: number fields for each tier, and a select for applies to. The function settings element plugs into the page's own Save button and Discard button. When the merchant clicks Save, we call apply metafield change: update metafield, dollar app, function configuration, our JSON.
The admin saves it together with the discount. No server of our own, no database. The settings live on the discount itself.
Real store demo
Our development store. Discounts, create discount. Under our app there is a new type: Volume tiers.
It opens the normal discount page, and there is our card. Automatic discount, and a title. Our tiers show their defaults: three, five and ten units.
Then I click Save.
A real error
But here is what happened the first time I clicked Save. There is one error with this discount: access to this namespace and key on metafields for this resource type is not allowed. The app owned namespace needs a definition before the admin may write to it.
You declare it in the app's TOML: discount, metafields, app, then the key. Type JSON. And admin access: merchant read write, so the settings card may save it.
One more catch: shopify app dev does not reload the app TOML. Stop it, start it again, and the definition appears. After that, Save works.
With the definition in place, the discount saves and it is active.
Tiers in the cart
Now the store. The everyday crew neck tee, $24. Three go into the cart.
5% off: twenty-two eighty each, sixty-eight forty in total. Plus one, still tier one. Plus one more: five shirts.
Buy five plus, save 10%. Twenty-one sixty each, $108.
The merchant edits it
Now I am the merchant. Back to the discount. Tier two, percentage off: twenty.
Save. That is it. No code, no build, no deploy.
Back in the cart. The page still shows 10%, because the cart has not changed since the last run. One more shirt, the Function runs again, and reads the new metafield.
Buy five plus, save 20%. Nineteen twenty each. $115 twenty for six shirts.
These are the real Function logs, shortened. Before the save: five shirts, tier two is 10%. After the save, the next run comes when the cart changes: six shirts, and the input already carries 20%.
Shopify runs the Function when the cart changes, not when you reload the page. So when you test new settings, change the
input_variables in action
cart. Now the collection filter. Applies to: collections.
Choose collections, search serums, and pick Serums and Face Oils. And I click Save. A thirty-eight dollar serum from that collection.
Three of them. Buy three plus, save 5%: thirty-six ten each. And twelve T-shirts.
Twelve would reach tier three, but the tee is not in Serums and Face Oils. So no discount: $288, full price. The real log of that run.
Shopify took the collection IDs from our metafield and passed them as the variable. So the input says: twelve tees, in any collection, false. And our output is empty.
The serum run said true, and got its 5%. That is what input variables are for: the merchant's settings change what the query asks Shopify. Measured on the real store runs: about 339,000 instructions, of the eleven million limit.
The module is two point eight kilobytes. 304 bytes in, 168 bytes out, for a one line cart. And the repository's GitHub Actions run builds and runs the Function with a real input, because Smart App Control still blocks function runner on this Windows
Common errors
PC. The errors, in one table. Access not allowed: add the metafield definition, and restart app dev.
New settings do not show: change the cart, the Function reruns then. JSON value is null: the merchant never saved, so keep defaults in your code, like we did. The deploy fails on variables: use input variables on the targeting block only.
And keep the settings small. Over 10,000 bytes, the Function does not get the value.
Recap
Let's recap. The settings are a JSON metafield on the discount, with a definition in the app's TOML file. The Function reads it as a JSON value, and through input variables for the collection filter.
The merchant edits it in a settings card, saved with apply metafield change. And our real cart went from five, to ten, to the merchant's 20%. The code is in the shopify functions 2027 repository, tag e p zero four, linked below.
In episode five, we build bundles with Cart Transform. See you there.
The Shorts from this video
Shopify Quantity Discount: Buy 3 Save 5%, Buy 5 Save 10% (Function)
Shopify quantity discount with one Discount Function: each cart line gets the biggest tier its quantity reaches. Real cart: 3 tees $72 → $68.40 (5%), 5 tees $120 → $108 (10%).
Shopify Discount UI Extension: Let Merchants Edit Your Function
Shopify discount UI extension: a settings card on the discount page (admin.discount-details.function-settings.render) saves the tiers into a JSON metafield with applyMetafieldChange; the Discount Function reads it. The merchant changes a tier, no deploy.
Shopify Discount Function Metafield Error: Access to This Namespace and Key Is Not Allowed
Shopify discount won't save with 'Access to this namespace and key on Metafields for this resource type is not allowed'? The app-owned $app metafield on the discount needs a definition: [discount.metafields.app.function-configuration] in shopify.app.toml (type json, admin merchant_read_write). Restart app dev, save again: it works.
🔔 New episodes every week on the YouTube channel. Need a custom Shopify app or API integration? Wixob Technologies builds them: info@wixob.com.