Shopify Functions: a free gift with a code, built as a discount Function

Topic
Development
Published
Reading time
8 min read
Written by
Binaery

Shopify Functions let a developer write the rule for a free gift instead of filling in a form. The built in Buy X get Y discount covers most offers. A discount Function is for the ones it can't describe: the gift is free only for certain customers, only when no subscription is in the cart, only one per order however the cart is arranged.

This one is for the developer on your team, or the founder who wants to know what they're asking for. By the end you'll know who can run a custom Function, what the code looks like, and the part a Function still doesn't do.

The short version

  • A discount Function is a small program that Shopify runs inside the cart and checkout to work out a discount. It lives in an app.
  • A public app that uses Functions can be installed on the Basic plan or higher. A custom app that uses Functions, built for your store alone, needs Shopify Plus.
  • The Function reads the cart, finds the gift line and returns one instruction: take 100% off this line.
  • It doesn't put the gift in the cart. The theme still has to do that, or the shopper does.
  • If the Function fails, Shopify drops the discount and lets checkout carry on. Nothing breaks, and nobody is told. Test it.
  • Start with the built in discount. Build this when you can name the rule the form can't express.

The heart of it is four lines. Find the gift in the cart, and do nothing if it isn't there:

const gift = input.cart.lines.find(
  (line) => line.merchandise.product?.hasAnyTag,
);
if (gift == null) return {operations: []};

Shopify hands the Function the cart as input. This looks through the cart's lines for a product carrying the tag you asked about, and if there isn't one it hands back an empty list, which means no discount.

When the built in discount is enough

Our article on setting up a free gift with a discount code covers the version with no code at all. It handles a minimum spend, a list of products or collections, eligibility by customer, segment or market, one use per customer and a cap per order. That's most gift offers.

Shopify's developer docs describe what Functions are for: discount behavior that isn't offered out of the box. The examples in the Discount Function API reference are the tell. Exclusions, where the discount skips some lines. Tiers that look at items, subtotal and delivery together. A discount for lines with a particular property, like an engraving.

Here's a rule of that kind, in a design. This is a bundle sheet we designed for a paint brush brand. Five of five items are in, a line says 10% and a saving of $6.14, and the total has come down to match. The saving depends on how full the bundle is, and the shopper is told why in words.

The bundle sheet showing the saving, as designed Paint brush brand The bundle sheet showing the saving, as designed

Who can run one

This is the first thing to check, because it decides the budget. From Shopify's help center, read on November 21, 2026:

Kind of app Basic and up Shopify Plus
A public app built with Shopify Functions, from the App Store Yes Yes
A custom app built with Shopify Functions, for one store No Yes

So on a lower plan the route is an app from the App Store whose rules happen to fit. A Function written for your store alone is a Shopify Plus job. Confirm that on Shopify's page on checkout apps and plans before anyone writes code.

The Function, piece by piece

A developer starts inside an app with one command from Shopify CLI, shopify app generate extension --template discount, which creates the files. Three of them matter.

The input query says which parts of the cart the Function wants to see. Shopify recommends asking only for what you need. Here we ask for each line's id and whether its product has the tag free-gift:

query CartInput {
  cart {
    lines {
      id
      merchandise {
        ... on ProductVariant {
          product { hasAnyTag(tags: ["free-gift"]) }
        }
      }
    }
  }
}

Tagging the gift product in the admin is what marks it. Change the gift next month and nobody touches the code.

The run function starts with the four lines at the top of this article, then returns the instruction:

return {operations: [{productDiscountsAdd: {
  candidates: [{
    message: "Free gift",
    targets: [{cartLine: {id: gift.id, quantity: 1}}],
    value: {percentage: {value: "100.0"}},
  }],
  selectionStrategy: ProductDiscountSelectionStrategy.First,
}}]};

In plain words: add a product discount, aimed at one unit of the gift line, worth 100%, with the message the shopper sees beside it. The number is written as text because that's the form Shopify's reference documents.

The configuration file ties the Function to its place in Shopify, a target named cart.lines.discounts.generate.run. The generator fills that in.

These pieces are checked against Shopify's reference as it stood today. A real build adds your own rule between finding the gift and returning the discount: the customer's tags, the other lines in the cart, the code that was entered. The reference lists the entered code as something the Function can read.

Turning it into a discount your team can use

Deploying the app gives the store a new discount type. Somebody still creates the discount itself, and that's what holds the code, the dates and the combination rules. A developer does it with a mutation called discountCodeAppCreate, or discountAutomaticAppCreate for one with no code, naming the discount class the Function returns. For a free gift that's the product class.

If your team should be able to set it up without a developer, the app also needs a small settings screen on the discount page. Shopify's docs describe two ways to build one. Budget for it. A Function only a developer can configure is a Function you'll call a developer about.

The part a Function doesn't do

A discount Function returns discounts. Its list of operations covers adding product and order discounts and accepting or rejecting entered codes. Adding a product to the cart isn't on the list.

So the gift still has to arrive in the cart, and that's the theme's job. When the cart qualifies, the theme adds the gift with Shopify's Cart API, the call themes use to add a product to the cart. The two halves then check each other. The theme can add the gift as eagerly as it likes, because the Function only makes it free when the rule is truly met. Anyone who adds the gift by hand without qualifying simply pays for it.

A worked example: the rule, and how to test it

Say the offer is this. Enter the code CANDLE and get one travel candle free, if the cart has $60 of full price candles and no subscription in it.

Cart What should happen
$64 of candles, the gift, the code Gift is free
$64 of candles, the code, no gift Theme adds the gift, then it's free
$58 of candles, the gift, the code Gift is charged
$64 of candles, a subscription, the gift, the code Gift is charged
$64 of candles, two gifts, the code One gift free, one charged
$64 of candles, the gift, no code Gift is charged

Write that table before the code. It's the brief, it's the test plan, and it's the document you'll want in February when somebody asks why a customer was charged for a candle.

What to know before you rely on it

  • A failed run is silent. If the Function fails, Shopify discards its output, doesn't apply the discount and lets checkout continue. That's the right call for the shopper, and it means a bug looks like a customer who didn't get their gift. Watch the Function's logs after launch.
  • It only controls its own discounts. A Function can't change Shopify's native discounts or another app's. How it combines with them is set on the discount, not in the code.
  • Functions don't know about each other. A store can have up to 25 discount Functions active, and they all run at the same time.
  • It's software. It needs an owner. Our own app, Atomato, uses a Shopify Function for the discounts in its product picks, and it gets looked after like the rest of the app.

Before you build one

  1. Run the offer with the built in Buy X get Y discount first, and write down what it couldn't do.
  2. Check your plan: a custom Function needs Shopify Plus.
  3. Look for a public app whose rules already fit.
  4. Write the table of carts and outcomes.
  5. Decide who sets the discount up each time: a developer, or your team through a settings screen.
  6. Build the theme half that adds the gift, and test every row of the table on a development store.
  7. After launch, read the Function's logs for the first week.

Common questions

Can Shopify Functions add a free gift to the cart automatically?

No. A discount Function calculates discounts for items already in the cart. It can make the gift line free, but it can't add the product. The theme adds the gift with Shopify's Cart API when the cart qualifies, and the Function makes it free only if the rule is met.

Do I need Shopify Plus to use Shopify Functions?

Not always. Public apps built with Shopify Functions can be installed on the Basic plan or higher. A custom app built with Shopify Functions, written for one store, is available only on Shopify Plus, according to Shopify's help center on November 21, 2026.

What is a Shopify discount Function?

A discount Function is a small program inside an app that Shopify runs in the cart and checkout to calculate a discount. It reads the cart, applies your rule and returns the discounts to apply to cart lines, the order or shipping. It lets a developer create discount types the Shopify admin doesn't offer.

When should I use a Function instead of a Buy X get Y discount?

Use Buy X get Y when the offer fits its form: a minimum spend or quantity, chosen products, chosen customers. Use a Function when the rule can't be expressed there, such as excluding certain cart lines, combining several conditions, or reading something particular about the customer or the cart.

Next step

Write the rule as one sentence, then as the table of carts. If every row could be handled by the Buy X get Y form, you don't need a Function, and you've saved yourself a project. If one row can't, send us the table. We build Functions and the apps around them, and we'll tell you plainly whether it's worth it. Tell us about the offer and one of us will write back within 48 hours.

Keep reading.

Rather have us do it?

Everything in this article is something we do for a living. Tell us about the store and what you're trying to move.

Get a quote