Shopify Functions Setup for Custom Discount/Shipping Logic

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Showing 1 of 1 servicesAll 2065 services
Shopify Functions Setup for Custom Discount/Shipping Logic
Complex
~5 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

Shopify Functions for Custom Discount and Shipping Logic

Shopify Functions — mechanism for running user code directly in Shopify infrastructure. Unlike webhook apps, Functions execute synchronously during checkout: Shopify calls the function, gets a response, and applies the result.

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 Shipping discounts
delivery_customization Hide/rename/sort delivery methods
fulfillment_constraints Warehouse split restrictions
order_routing Route orders to warehouses

Functions written in Rust (official language, compiles to WebAssembly) or JavaScript/TypeScript (Javy runtime). Rust preferred for complex logic—faster and more economical.

Installation and Initialization

shopify app generate extension --template discount --name custom-volume-discount
cd extensions/custom-volume-discount

Structure: src/run.ts (or run.rs), input.graphql, shopify.extension.toml, package.json.

Example: Volume Discount Function (JavaScript)

input.graphql describes what data Shopify passes:

query RunInput {
  cart {
    lines {
      id
      quantity
      merchandise {
        ... on ProductVariant {
          id
          product {
            id
            tags
          }
        }
      }
    }
  }
}

src/run.ts function logic:

export function run(input: RunInput): FunctionRunResult {
  const discounts = [];

  for (const line of input.cart.lines) {
    const variant = line.merchandise;
    if (variant.__typename !== "ProductVariant") continue;

    if (!variant.product.tags.includes("volume-eligible")) continue;

    discounts.push({
      targets: [{ cartLine: { id: line.id } }],
      value: { percentage: { value: String(line.quantity > 5 ? 10 : 5) } },
      message: `${line.quantity > 5 ? 10 : 5}% bulk discount`,
    });
  }

  return { discounts, discountApplicationStrategy: "FIRST" };
}

Activation via Shopify Admin

Functions don't work automatically—create a Discount in Admin and select the function:

const CREATE_DISCOUNT = `
  mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) {
    discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) {
      automaticAppDiscount { discountId title startsAt }
      userErrors { field message }
    }
  }
`;

Function Limitations

  • Execution time: 5ms for Rust/WASM, 10ms for JS
  • Memory: 10 MB
  • No network requests: all data via input.graphql
  • No database access: complex config via Metafields
  • No Checkout UI Extensions: separate mechanism

Testing

npm run test
shopify app deploy
shopify app logs

Timeline

Simple discount function (percentage by tag/collection): 2–4 days. Complex multi-level function with metafield config and Admin UI: 1–2 weeks. Shipping function with geologic and external tariff integration: 1–2 weeks.