What are Shopify Discount Functions?
Shopify discount functions are a specific type of Shopify Function that calculates and applies discounts directly within Shopify's own checkout and cart flow. Instead of bolting an external app on top of checkout, you place your discount logic inside the platform as code that runs when a customer adds items to the cart or moves to payment. This means the discount is calculated on Shopify's infrastructure rather than in a third-party service that has to be called from outside.
A single discount function can target three levels: discounts on individual cart lines, discounts on the order subtotal, and discounts on the shipping rate. That covers the vast majority of discount types brands actually need: volume discounts, bundles, free shipping over a threshold, customer-specific pricing, and campaigns that apply only to certain collections.
How do discounts work in Shopify, and where do functions come in?
How do Shopify discounts work in practice? Standard discounts cover percentage and fixed-amount discounts, free shipping, and buy-x-get-y, either as discount codes the customer enters or as automatic discounts triggered by rules. That is perfectly fine for simple campaigns. But the moment the logic becomes conditional, for example "15 percent off category A, but only if the cart contains at least two items from category B, and only for the VIP segment", standard discounts hit a limit.
This is exactly where discount functions come in. A function takes the full context of the cart as input, customers, products, quantities, metafields, and returns which discounts should be applied and how they are allocated. In other words, you can express rules that cannot be clicked together in the admin. The result displays correctly in the cart and at checkout, and since 2024 discounts built with discount functions can also be applied in Shopify POS, so your online and physical stores follow the same logic.
How to discount a product with a function
How do you discount a specific product with a function rather than an ordinary discount? You target a cart line discount at the products that match your criteria. The criteria can be product ID, collection, tags, or metafields, and the discount itself can be a percentage or a fixed amount per line. The advantage is that the logic lives in one place and is testable, instead of being spread across several overlapping automatic discounts that are hard to unpick.
A recurring question is discount allocation, the function in Shopify that determines how a discount is distributed across the cart's lines. When an order discount of, say, 200 kr. needs to be deducted, Shopify distributes the amount proportionally across the relevant lines so that VAT, returns, and accounting reconcile line by line. With discount functions you control which lines are eligible for a discount at all and leave the precise distribution to Shopify's allocation logic, so the numbers always add up.
How Mercive approaches complex discount rules
Our starting point at Mercive is simple: use standard discounts as far as they will take you, and only build a function when the business logic requires it. Many brands install a heavy discount app for a need that a well-scoped function can cover faster, more cheaply to run, and without an extra subscription or performance overhead in checkout.
When we build a discount function, we start by mapping the actual discount scenarios and their prioritization: what happens when several campaigns overlap, which discounts may be combined, and what the desired behavior is at the edge. We then implement the logic as code, connect it to product and customer data via metafields, and test it against realistic carts, including POS. The approach is part of our work with checkout extensibility and our ongoing recommendations to move logic out of apps and into the platform.
When a function is the right solution
A discount function makes the most sense when the discount is conditional, segmented, or needs to be combined in a way the admin does not support, and when you want to own the logic rather than rent it through an app. For brands with complex B2B pricing and tiered structures, a function keeps the rules consistent, testable, and maintained in one place across both online and physical sales.
How Mercive can help
Want to take this further? Read more here:

