HomeBlogBuilding Custom Shopify Functions for Complex B2B Tiered Pricing & Dynamic Discounts
Back to all playbooks
App Dev

Building Custom Shopify Functions for Complex B2B Tiered Pricing & Dynamic Discounts

Learn how Shopify Functions replace legacy Shopify Scripts to execute backend pricing rules, volume discounts, and custom cart validations in sub-5ms WebAssembly.

MN

Morshadun Nur

Founder of StoresForge, showform.app and Qbytesoft.com

August 17, 20269 min read

For enterprise e-commerce and B2B wholesale brands, standard discount codes and basic catalog rules are rarely sufficient. Merchants routinely require complex backend logic such as:

  • Tiered Volume Breaks: Buy 10-49 units get 15% off; buy 50+ units get 30% off.
  • Customer Segment Specific Rules: Wholesale VIP customers receive net-30 terms and custom contract pricing while retail customers see standard MSRP.
  • Cross-Category Product Bundles: Buy any jacket + pants and get a free base layer, with automatic cart price adjustments.
  • Cart & Payment Validation: Restrict hazardous or perishable items to specific domestic delivery zones and payment methods.

In the past, Shopify Plus merchants relied on Ruby-based Shopify Scripts. However, Scripts were limited strictly to Plus, ran on legacy infrastructure, and could not be configured through a standard Shopify App interface.

Enter Shopify Functions: Shopify's next-generation platform for backend logic customization.

---

What Are Shopify Functions?

Shopify Functions allow developers to write custom backend business logic that compiles to WebAssembly (WASM). These functions execute directly inside Shopify's core checkout and cart infrastructure in under 5 milliseconds, scaling seamlessly through high-traffic flash sales and BFCM traffic spikes.

Key Function APIs available today include:

  1. Discounts API (Product, Order, Shipping Discounts): Create bespoke volume discounts, BOGO rules, and dynamic spend milestones.
  2. Cart & Checkout Validation API: Block checkout progression if minimum order values or regional compliance criteria are not met.
  3. Cart Transform API: Merge individual items into bundled parent products or expand a single line item into multiple kit components.
  4. Payment & Delivery Customization API: Reorder, hide, or rename shipping methods and payment gateways based on customer tags or cart contents.

---

Anatomy of a Custom B2B Tiered Pricing Function

When engineering a custom Shopify Function with Rust or JavaScript (via javy), the function follows a clean 3-step lifecycle:

`mermaid graph LR A[GraphQL Input Query] --> B[WASM Business Logic Execution < 5ms] B --> C[Structured JSON Function Output] `

#### 1. Input Query (run.graphql) The Function queries real-time cart state, customer tags, and custom metafields configured in the Shopify Admin:

`graphql query RunInput { cart { buyerIdentity { customer { hasAnyTag(tags: ["B2B_Wholesale_Tier1", "B2B_Wholesale_Tier2"]) } } lines { id quantity cost { subtotalAmount { amount } } merchandise { ... on ProductVariant { id product { inAnyCollection(ids: ["gid://shopify/Collection/123456789"]) } } } } } } `

#### 2. Business Logic Execution (run.ts / run.rs) The function evaluates the quantities against configured volume brackets stored in app metafields:

`typescript export function run(input: RunInput): FunctionRunResult { const isWholesale = input.cart.buyerIdentity?.customer?.hasAnyTag; if (!isWholesale) { return { discountApplicationStrategy: DiscountApplicationStrategy.First, discounts: [] }; }

// Calculate volume tiered percentage const totalQty = input.cart.lines.reduce((sum, line) => sum + line.quantity, 0); let discountPercentage = 0;

if (totalQty >= 50) discountPercentage = 30; else if (totalQty >= 20) discountPercentage = 20; else if (totalQty >= 10) discountPercentage = 10;

if (discountPercentage === 0) { return { discountApplicationStrategy: DiscountApplicationStrategy.First, discounts: [] }; }

return { discountApplicationStrategy: DiscountApplicationStrategy.First, discounts: [ { value: { percentage: { value: discountPercentage.toString(), }, }, targets: input.cart.lines.map((line) => ({ cartLine: { id: line.id }, })), message: B2B Tiered Volume Discount (${discountPercentage}%), }, ], }; } `

#### 3. Output Shopify's checkout engine receives the output and updates prices instantly, showing the custom discount message across all checkout stages and order confirmations.

---

Advantages of Shopify Functions vs. Third-Party Pricing Apps

| Feature | Legacy Third-Party Apps | Custom Shopify Functions | | :--- | :--- | :--- | | Execution Speed | 500ms - 2000ms (Client-Side JS API calls) | < 5ms (Native WASM inside Shopify Engine) | | Checkout Compatibility | Frequently breaks during 1-Click Shop Pay / Apple Pay | 100% Native & Guaranteed Compatible | | Monthly Cost | $199 - $899 / month recurring subscription fees | $0 Monthly Fees (One-time asset built for your store) | | Theme Code Cleanliness| Injects heavy external DOM scripts and tracking cookies | Zero Theme Code Bloat |

---

> [!TIP] > Need a custom pricing engine, ERP sync, or private Shopify app? > Explore our Shopify App Development Services and Shopify Plus Solutions. Check our verified developer profile on Upwork and live public apps on the Shopify App Store.

---

Conclusion

Shopify Functions represent the modern gold standard for building tailored e-commerce business logic. By moving complex pricing, bundling, and validation rules into native WebAssembly functions, you eliminate recurring app fees and deliver lightning-fast checkout experiences to your buyers.

Schedule a technical consultation with StoresForge to architect custom Shopify Functions for your store today.

#Shopify Functions#Shopify Plus#B2B#App Development#Custom Discounts
Share this Playbook

Disclaimer: The engineering playbooks, benchmarks, and strategies shared here are property of the StoresForge performance optimization division and represent verified production outcomes. Individual store results may vary based on exact theme architecture, app installations, and API usages.