A Developer’s Guide to Shopify Discount Functions

Diksha P
March 12, 2025
Share article
Supercharge Sales with Kite!
Boost sales with Free Gifts, Discounts, and BOGO offers. Customize easily and get 24/7 support.
Free gifts & discounts
BOGO offers
Checkout the App
Thank you, we'll get back to you!
Oops! try again
B
o
o
k
 
a
 
D
i
s
c
o
v
e
r
y
 
C
a
l
l
We got you! Book a call
with us
Oops! try again
Book a Discovery Call
Supercharge Sales with Kite!
Supercharge Sales with Kite!
Free gifts & discounts
BOGO offers

As a Shopify developer, implementing discount features can be a tricky process, especially when it comes to choosing between & implementing different methods. 

In this article, we’ll explore what Shopify Discount Functions are, how they differ from Shopify’s native ability to run discounts, and also guide you through the migration to discount functions. 

We'll also cover how you can create your own discount functions, both with code and without, using apps that integrate seamlessly with Shopify. So dive right in

Shopify Functions 

To understand Shopify Discount functions, let’s first understand what Shopify Functions are.

​Shopify Functions are custom server-side scripts that execute in response to specific events within the Shopify platform. They enable developers to extend or modify Shopify's discount backend logic to cater to unique business requirements.​

What are Shopify Discount Functions? 

Discount Functions are a specific type of Shopify Function designed to calculate and apply discounts during the checkout or cart evaluation process. 

  • Unlike client-side scripts or external APIs, these functions run on Shopify's servers, ensuring seamless integration with the checkout flow. 
  • They process structured inputs, such as cart contents and customer information, execute custom discount logic, and return results that Shopify uses to apply the appropriate discounts. ​
  • Shopify automatically invokes these functions during relevant events, such as when a cart is updated or a discount code is applied. 

Why use Discount Functions? 

Minimal Latency =  Better Customer Experience: Developers do not need to manually trigger discount functions via API calls; instead, they operate inline within the checkout process, ensuring minimal latency and a smooth customer experience.​

Beyond Shopify Native: By using Discount Functions, developers can introduce new discount types and promotions beyond Shopify's native capabilities. These functions execute rapidly, typically in under 5 milliseconds, on Shopify's global infrastructure, ensuring real-time performance during both cart and checkout operations.

Technical Implementation: Discount Functions

Here’s how you go about implementing them technically: 

A. Types of Shopify Functions - Discount API 

Shopify offers three main discount function APIs as of now, corresponding to different discount scopes:

1. Product Discount Function API

Allows discounts on specific products, variants, or individual cart lines​. This creates new discount types that apply to particular items in the cart. Example use cases: “Buy 2 of this product, get 10% off those items” or "$5 off blue T-shirts" (targeting a product variant)​.

Product Discount Function API
  • The function can iterate over cart lines and apply a discount to those matching certain criteria (product ID, variant, quantity threshold, etc.). 
  • Each discount can target either specific cart line IDs or all items of a certain product variant​.

2. Order Discount Function API

Allows discounts on the entire cart/order subtotal​. These functions produce an order-level discount (affecting all eligible items in the cart). Example use cases: "10% off the whole order if cart total exceeds $100" or "Spend $200, get $25 off the entire order"​.

Order Discount Function API
  • This is useful for tiered cart-wide promotions or conditional order discounts that aren’t natively supported. 
  • For instance, you could create separate order-discount instances per market: one for the US and one for Canada with different thresholds, allowing currency-specific promotion rules​.

3. Shipping Discount Function API

Allows discounts on shipping rates at checkout​. These functions can modify the pricing of shipping options. Example use cases: "Free shipping on orders over $X" or "20% off Express Shipping for VIP customers"​.

Shipping Discount Function API
  • You could even target specific shipping methods, e.g., apply 50% off only to the Standard Shipping rate during a holiday promotion​. 
  • The function output will specify which shipping option(s) to discount and by how much.

B. How are they Deployed? 

Each discount function is deployed as an app extension. When you build a Shopify app, you use the Shopify CLI to scaffold a function of the desired type (product, order, or shipping discount). For example, to start a product discount function, you might run:

shopify app generate extension --template=product_discounts --name="volume-discount"

This creates a function project with a configuration file, a run.graphql (to define the input query for the function), and a run.js or run.rs (the function logic in JavaScript or Rust). Shopify Functions can be written in any language that compiles to WebAssembly, though Shopify provides tooling for Rust and JavaScript (TypeScript) out of the box​. 

Rust is recommended for better performance on large carts​, but TypeScript/JavaScript templates are available for convenience.

How Discount Functions Work on Cart and Checkout

Once the function code is written and deployed to Shopify’s servers (via the app installation on a store), it will execute automatically during checkout/cart calculations. 

For example, if you created a volume discount function (a product discount type) that gives 10% off on buying 2 or more of a product, Shopify will run your function whenever a customer’s cart is updated (adding items, changing quantity) to see if the conditions are met and then apply the discount. 

The function receives data like the cart lines (with product IDs, quantities, attributes), customer info, cart attributes, etc., based on the GraphQL RunInput you defined​. It then returns a result specifying the discounts to apply.

API References and Activating Discount Functions

After deploying a function through your app, you need to activate it by creating a discount that uses the function. Shopify provides GraphQL Admin API mutations for this. In your app (or using the Shopify GraphiQL explorer), you would call:

  • "discountAutomaticAppCreate" for automatic discounts (usually added on cart directly)
  • "discountCodeAppCreate" for discount codes tied to the function logic (usually entered on checkout)

These mutations let you register a new discount entity in the store that links to your function by its ID​. For example, you could create an automatic discount that applies your “volume discount” function site-wide or create a discount code (e.g. SAVE10) that, when entered, will trigger your function’s logic to calculate the discount. 

In either case, Shopify needs a record of a discount in the admin that corresponds to your function. The API call typically includes details like the function ID, the title of the discount, the strategy for how it combines with other discounts, etc. (When registering via GraphQL, you can specify combination behavior, for instance, whether the function’s discount can stack with another discount code or if it should be exclusive​. 

To use these Admin API mutations, your app must have the write_discounts permission scope​. Also, only Shopify Plus stores could historically use custom apps for such features, but with Shopify Functions, even non-Plus merchants can install public apps from the App Store that contain Functions​. This opens up advanced discounts to all stores.

Code Snippet Example – Implementing a Volume Discount

Below is a simplified example of a Product Discount Function that gives a 10% discount on any cart line where the quantity is 2 or more (a basic volume discount). This example is written in a JavaScript-style pseudocode for illustration:

/**
 * @param {RunInput} input - Shopify provides cart data as input to the function.
 * @returns {FunctionRunResult} - The function must return a result with discounts.
 */
export function run(input) {
  const result = {
    // Strategy: apply this discount first (before any others)
    discountApplicationStrategy: "FIRST",  
    discounts: []  // will fill with any applicable discounts
  };
  // Loop through all cart lines in the input
  for (const line of input.cart.lines) {
    if (line.quantity >= 2) {
      // If this cart line has 2+ of the same item, apply 10% off that line
      result.discounts.push({
        targets: [
          { cartLine: { id: line.id } }  // target this specific cart line
        ],
        value: {
          percentage: { value: 10 }      // 10% off (percentage value)
        },
        message: "10% off for 2+ qty"     // text that will show in checkout
      });
    }
  }

  return result;
}
Implementing a Volume Discount

In this snippet, the function inspects each cart line; if the quantity meets the condition, it appends a discount to the result. The FunctionRunResult includes an array of discounts, each with a target (here, we target the specific cart line by ID) and a value (10% off). The message is optional and if provided, will be displayed to the customer (e.g., “10% off for 2+ qty” shown under the line item). Shopify will take this output and apply the discounts accordingly in the checkout price calculations.

Under the hood, the data structures and types (like RunInput, FunctionRunResult, Discount, CartLineTarget, etc.) are defined by Shopify’s schema​. For example, a discount can target either specific cart lines or entire product variants, and you can even limit the number of items to which the discount applies by using the quantity field on the target​.

In our example, we targeted each cart line individually; alternatively, one could target a product variant ID to apply a rule to all lines of that variant in the cart (useful for variant-specific promotions).

Shopify Discount Functions vs Shopify Scripts vs Native Shopify Discounts

Shopify offers three distinct methods for implementing discounts: Native Shopify Discounts, Shopify Scripts, and Shopify Discount Functions. Understanding the differences among these options is crucial for developers aiming to optimize discount strategies and enhance the scalability of Shopify stores.​

Out of the box, Shopify provides basic discount types (like automatic discounts or discount codes for percentage, fixed amount, BOGO, etc.). Shopify Discount Functions extend this by enabling completely custom discount rules that native discounts can’t do. 

For example, with a discount function you could implement volume pricing with tiered rates, “buy X get Y at 50% off” deals, customer-segment specific discounts, etc. – all of which are defined by code. When a merchant installs an app containing a discount function, the new discount type becomes available in their Shopify Admin (just like built-in discounts) and can be configured without touching code​. 

This means developers can create unique discount logic, and merchants can use it via the familiar Discounts interface in Shopify Admin.

Let’s learn more about the three different ways to implement Shopify Discount functions

1. Native Shopify Discounts

These are built-in discount features provided by Shopify, allowing merchants to offer various promotions without additional coding and in the Shopify Admin itself.

Native Shopify Discounts

Discount methods:

  • Automatic Discounts: Applied automatically at checkout when certain conditions are met.​
  • Discount Codes: Require customers to enter a code to receive a discount.​

Customization:

Limited to predefined discount types such as percentage off, fixed amount off, or free shipping.​

Basic rules based on minimum purchase amounts, specific products, collections, or customer segments.​

Management:

Configured through the Shopify Admin interface, offering a user-friendly setup without the need for coding.​

2. Shopify Scripts

Introduced for Shopify Plus merchants, Scripts allow for customizations within the checkout process using a Ruby-based scripting language.​

Shopify Scripts

Functionality:

Modify line item properties, shipping rates, and payment methods during checkout and apply discounts based on complex conditions, such as customer tags or cart contents.​

Customization:

Offers advanced customization capabilities, including conditional logic and personalized promotions and requires knowledge of Ruby programming.​

Limitations:

  • Exclusive to Shopify Plus plans.​
  • May face performance constraints with complex scripts.​
  • Shopify scripts are getting deprecated and no longer supported by Shopify starting from August, 2025.

3. Shopify Discount Functions

Discount Functions enable developers to create custom discount logic that integrates seamlessly with Shopify's ecosystem. This is built on the latest technology and is supported to scale by Shopify. 

Functionality:

  • Create advanced discount rules, such as tiered pricing, "buy X get Y" offers, and customer-segment-specific discounts.​
  • Apply discounts automatically or via discount codes.​
  • Can be created using No code through apps built on discount functions.

Customization:

  • Provides granular control over discount application, supporting dynamic conditions based on real-time factors like inventory levels or customer behavior.​
  • Developed using Shopify's Functions framework, offering a flexible and scalable approach.​
  • Available to everyone, unlike Scripts which are exclusive to stores on Shopify plus.

Management:

Once the app(s) built on discount functions are installed, new discount types become available in the Shopify Admin interface, allowing merchants to configure them without touching code.​

Kite Admin Panel

Comparative Overview

The following table summarizes the key differences among Native Shopify Discounts, Shopify Scripts, and Shopify Discount Functions:

Feature Native Shopify Discounts Shopify Scripts Shopify Discount Functions
Customization Level Basic (percentage, fixed amount, free shipping) Advanced (Ruby scripting for complex logic) Advanced (custom code for unique discount logic)
User Interface Shopify Admin Shopify Admin with script editor Apps using Discount functions in Shopify Admin
Required Expertise None Ruby programming knowledge Familiarity with Shopify Functions framework
Performance Standard May vary with script complexity Optimized for scalability and performance
Availability All Shopify plans Shopify Plus only.
Gets deprecated on August 2025
Available across all plans with checkout extensibility exclusive to Plus
Use Cases Simple discounts based on basic conditions Custom checkout experiences and promotions Complex, dynamic discounts with real-time adjustments
Management Easy setup and management through Admin Requires ongoing maintenance and testing Managed through Admin via Apps running discount functions with intuitive interface & support

Migrating from Shopify Scripts to Functions

As Shopify transitions from Scripts to Functions, it's essential for developers to understand the migration process to ensure continued customization capabilities for their stores.​

Why Migrate?

Shopify has announced that Scripts will be deprecated on August 28, 2025. This change necessitates migrating existing customizations to Shopify Functions, which offer enhanced performance, scalability, and flexibility.​

Getting Started with Migration

  1. Review Current Scripts: Identify all active Scripts in your store by navigating to the Script Editor in your Shopify admin. Understand their functionality and note any complex conditions or calculations.​
  2. Understand Shopify Functions: Familiarize yourself with Shopify Functions, which are custom server-side scripts that execute in response to specific events within Shopify. They allow for more advanced and scalable customizations compared to Scripts.​
  3. Recreate Customizations: Translate your existing Scripts into Functions. Shopify provides documentation and tutorials to assist with this process.​
  4. Test Thoroughly: Before deactivating your Scripts, ensure that the new Functions replicate the desired behaviors accurately.​

To know more, read our detailed guide to migrating from Shopify Scripts to Shopify Functions. 

Note: It's important to complete the migration before August 28, 2025, to maintain uninterrupted customization support.

How can you run Shopify Discount functions without using Code?

For merchants who prefer not to dive into coding, several Shopify apps facilitate the creation and management of Discount Functions through intuitive interfaces. These apps allow you to set up complex discount rules without writing a single line of code.​

Use an App Like Kite: Discounts & Free Gift

At Kite: Discount & Free Gift, we enable merchants to design and implement various discount strategies effortlessly and without using any code. 

With Kite, you can create:​

  • Discounts: Set up percentage-based or fixed-amount discounts on specific products or collections.
  • Custom discounts: Create almost any type of discount offer in the wild, and mix and match such discounts to run together seamlessly.
  • Free Gifts and bogo: Offer complimentary products when customers meet predefined purchase criteria.​
  • Shipping discounts: Run discounted or free shipping on certain shipping methods or all
  • Multiple language support & eligibility: Decide what customer segment or countries are able to see and use the offers.

The app's user-friendly interface guides you through the process of defining discount conditions, target products, and promotional messages, ensuring a seamless setup without the need for coding knowledge.

Case Study: Read how Evelyn & Bobbie Increased AOV with Buy X Get Y Discount

Limitations of Discount Functions

While Shopify Discount Functions offer robust customization capabilities, it's important to be aware of certain limitations, especially when dealing with large inventories and complex discount structures.

  • Large Cart Quantities: Applying discounts to large quantities can lead to performance issues. It's essential to optimize your discount logic to handle substantial cart sizes efficiently.​
  • Line Items: Managing discounts across multiple line items, especially when items are split or duplicated during checkout, can be challenging. Careful configuration is required to ensure discounts apply correctly without causing discrepancies. ​
  • Discounts on Large Collections: Implementing discounts across extensive product collections may encounter limitations. For instance, applying discounts to a collection that has thousands of products could lead to failure in the discount functions getting applied.

How to Tackle the Limitations?

To address these challenges:

  • Optimize Discount Logic: Ensure that your discount functions are designed to handle large datasets efficiently, minimizing performance bottlenecks.​
  • Utilize Specialized Apps: Leverage apps like Kite: Discount & Free Gift, which offer advanced features & optimizations to manage complex discount scenarios & larger carts without the need for custom coding.​
  • Custom Development: For unique requirements, consider developing custom functions or consulting with apps that have custom discount features like Kite: Discount & Free Gift.
  • Using Rust over JS: Programming languages like Rust provide better optimized and efficient ways of applying the discount codes.

Conclusion

As a Shopify developer, navigating discount implementations can be challenging, but Shopify Discount Functions offer a powerful, future-proof solution. 

With Shopify Scripts being phased out, now is the time to transition to a more flexible and scalable approach. These functions give you full control over discount logic, ensuring better performance and a smoother customer experience.

While there might be some limitations, you can use other methods, such as Rust or even no-code apps (Kite: Discount & Free Gift App) to circumvent this. 

Embracing Discount Functions now will not only keep the stores you build or maintain ahead but also unlock new possibilities for you. 

As a Shopify developer, implementing discount features can be a tricky process, especially when it comes to choosing between & implementing different methods. 

No items found.

Rushy Scarcity Countdown Timer

Free to install
Built for Shopify

Hurrify customers to buy within a given timeframe with a sales countdown timer & improve conversions

Another popular Shopify checkout app is Checkout Promotions. The app comes with the ability to leverage a collection of highly robust visibility rules that help show customers one-click post purchase upsell promotions after an order payment has been made. Some of its key features include:

Features

AI recommended and manual recommendations for upselling.

Complete branding control.

Checkout Upsell for increasing AOV.

AI recommended and manual recommendations for upselling.

Pricing

Development

Free

Monthly Plan

$99/ month

Plus Plan

$99/ month

Plus Plan

$99/ month

FAQs on Shopify Discount Functions

What are some Shopify apps built on Shopify Discount Functions?
How many discount codes can I create on Shopify?
What are the limitations of Shopify Discount Functions?
How to enable Shopify Functions?
What is the difference between discount codes and automatic discounts?
How do Shopify Functions work?
Diksha P

Diksha P

Diksha leads Product Management at Skai Lama. She talks about discount campaigns, product recommendations, quizzes, and surveys to personalize Shopify stores.
Supercharge Sales: Free Gifts, Discounts, BUy X Get Y & BOGO Offers
Supercharge Sales: Free Gifts, Discounts, BUy X Get Y & BOGO Offers
All In One Free Gifts & Discount App. Customize easily and get 24/7 support.Try now to transform your store’s success!
All In One Free Gifts & Discount App. Customize easily and get 24/7 support.Try now to transform your store’s success!
We got you! Book a call
with us
Oops! try again
Book a Call
We got you! Book a call
with us
Oops! try again
Book a Call

Access practical strategies and execution tips, to set up your store for success.

View Guide