Custom Discount and Shipping Logic with Shopify Functions
Imagine an online clothing store that wants to offer a 15% discount on the second item in the cart, but only for customers in Moscow and when the total is at least $60. Standard Shopify discounts can't check geolocation or consider item-level cart composition. The solution is Shopify Functions — synchronous hooks that run during cart calculation without leaving the Shopify infrastructure. In 5 ms, we can apply complex discount or shipping rules without increasing checkout time. According to Shopify, up to 20% of users abandon a checkout if it takes more than 2 seconds, so optimizing Shopify Checkout with custom functions is becoming a standard for large stores. Our team sets up such functions turnkey: from simple percentages to complex geo-based shipping. Development cost is individual, savings compared to external microservices reach 30%, and average order value increases by 15–25% after implementing personalized discounts.
Why Shopify Functions Are Faster Than External APIs
Functions run inside the checkout process — they are not external APIs making HTTP requests. They compile to WebAssembly (Rust) or use the Javy runtime (JavaScript) and execute within a 5–10 ms limit. No network calls, all data is passed via input.graphql. This guarantees predictable performance and zero user-perceived latency. More on the architecture in the Shopify Functions documentation.
Where Functions Work
| Function API | Purpose |
|---|---|
cart_transform |
Modify cart lines before checkout (bundles, modifications) |
discounts |
Custom discounts (fixed, percentage, BXGY) |
payment_customization |
Hide/rename payment methods |
shipping_discount |
Discount on shipping |
delivery_customization |
Hide/rename/sort delivery methods |
fulfillment_constraints |
Constraints on split shipments |
order_routing |
Route orders to warehouses |
Functions are written in Rust (compiled to WebAssembly) or JavaScript/TypeScript (via Javy runtime). Rust is preferred for complex logic — faster and more limit-efficient. Shopify Functions development includes designing input.graphql and Metafields.
How to Implement Complex Discounts Without Performance Loss
We use Rust or TypeScript, optimizing every query. All data preparation is offloaded to the Metafields level — the function receives ready-made configs and applies them in milliseconds. Our experience shows: with proper design, a function fits within 5 ms even with 5 nesting levels. In one project, we implemented a discount factoring order history via Metafields: the function loaded a JSON with thresholds for the last 30 days and applied an increased discount for loyal customers — all within the 4 ms limit. Configuring Shopify discounts with Functions allows implementing any business rules without slowing down checkout.
Example: Volume Discount (JavaScript)
input.graphql — describes what data Shopify will pass to the function:
# input.graphql query RunInput { cart { lines { id quantity merchandise { ... on ProductVariant { id product { id tags metafield(namespace: "custom", key: "volume_discount_tier") { value } } } } } } discountNode { metafield(namespace: "custom", key: "tiers") { value } } } src/run.ts — function logic:
import type { RunInput, FunctionRunResult } from "../generated/api"; interface DiscountTier { quantity: number; percentage: number; } export function run(input: RunInput): FunctionRunResult { const tiersJson = input.discountNode?.metafield?.value; const tiers: DiscountTier[] = tiersJson ? JSON.parse(tiersJson) : []; if (!tiers.length) return { discounts: [], discountApplicationStrategy: "FIRST" }; const discounts = []; for (const line of input.cart.lines) { const variant = line.merchandise; if (variant.__typename !== "ProductVariant") continue; const product = variant.product; if (!product.tags.includes("volume-eligible")) continue; const applicableTier = tiers .filter(t => line.quantity >= t.quantity) .sort((a, b) => b.quantity - a.quantity)[0]; if (!applicableTier) continue; discounts.push({ targets: [{ cartLine: { id: line.id } }], value: { percentage: { value: String(applicableTier.percentage) } }, message: `Discount ${applicableTier.percentage}% for quantity (from ${applicableTier.quantity} pcs.)`, }); } return { discounts, discountApplicationStrategy: "FIRST", }; } How to Set Up Geolocation-Based Shipping
We use the delivery_customization API. The function receives the shipping address via input.graphql and can hide or reorder shipping methods. For example, for remote regions with long delivery times, we hide express methods. The logic is written in Rust for maximum speed — typical execution time is 3 ms. For integration, contact our engineers. Custom shipping with geolocation is one of the common uses of Functions.
Shopify Shipping Constraints
| Constraint | Value | Solution |
|---|---|---|
| Execution time | 5 ms (Rust/WASM), 10 ms (JS) | Minimize iterations, cache data in Metafields |
| Memory | 10 MB | Do not load large JSON arrays — use pagination at query level |
| Network requests | Forbidden | Pass all data via input.graphql |
| Database access | None | Store complex configs in Metafields |
Process
- Analysis — break down business rules, design input data.
- Design — choose language and optimize queries for limits.
- Implementation — write code, prepare input.graphql and Metafields.
- Testing — locally via
npm testorcargo test, then on staging. - Deploy — via Shopify CLI, activate in Admin.
Timelines depend on complexity: simple discount from 2 business days, function with geo logic up to 2 weeks.
Testing and Deployment
npm run test # or cargo test shopify app deploy shopify app logs # view logs Additional debug commands
shopify app log --tail What Our Work Includes
- Designing function architecture with limits in mind
- Writing code in Rust or TypeScript
- Developing input.graphql and Metafield schemas
- Deploying and integrating with Admin
- Documentation on rule configuration
- Post-launch support (1 month)
Our team consists of certified Shopify developers with 5+ years of experience and 30+ implemented projects. We will assess your task in 1 business day. Contact us — we will help implement custom discount and shipping logic that doesn't slow down checkout. Get a consultation.







