Shopify functions

From OrcaQubits/agentic-commerce-skills-plugins · View interactive version

Shopify functions is an AI agent skill from the GitHub repository OrcaQubits/agentic-commerce-skills-plugins. Build Shopify Functions — serverless WebAssembly extensions for discounts, delivery customization, payment customization, cart validation, cart transforms, and order routing. Use when extending Shopify's backend logic.

How to use this skill

Skill file

Shopify Functions (Serverless Wasm Extensions)

Before writing code

Fetch live docs:

  1. Fetch https://shopify.dev/docs/apps/build/functions for Functions overview
  2. Web-search site:shopify.dev shopify functions input output for I/O schemas
  3. Web-search site:shopify.dev shopify functions api reference for function types

What Are Shopify Functions

Serverless extensions that run on Shopify's infrastructure:

Function Types

| Type | Purpose | Example |

|------|---------|---------|

| Discount | Custom discount logic | Buy X get Y, tiered discounts |

| Delivery customization | Modify shipping options | Rename, reorder, hide methods |

| Payment customization | Modify payment methods | Hide, reorder, rename gateways |

| Cart validation | Block or warn on cart conditions | Quantity limits, product combos |

| Cart transform | Modify cart contents | Bundle expansion, auto-add items |

| Fulfillment constraints | Control fulfillment behavior | Location priority, restrictions |

| Order routing | Direct orders to locations | Closest warehouse, priority |

Input/Output Model

Functions receive structured JSON input and return structured JSON output:

Input (simplified)

{
  "cart": {
    "lines": [
      {
        "id": "gid://shopify/CartLine/1",
        "quantity": 2,
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/123",
          "product": {
            "id": "gid://shopify/Product/456",
            "hasAnyTag": true
          }
        },
        "cost": {
          "amountPerQuantity": { "amount": "29.99", "currencyCode": "USD" }
        }
      }
    ]
  },
  "discountNode": {
    "metafield": {
      "value": "{\"percentage\": 10}"
    }
  }
}

Output (discount example)

{
  "discounts": [
    {
      "value": { "percentage": { "value": "10.0" } },
      "targets": [
        { "productVariant": { "id": "gid://shopify/ProductVariant/123" } }
      ],
      "message": "10% loyalty discount"
    }
  ],
  "discountApplicationStrategy": "FIRST"
}

Project Structure

extensions/my-discount/
├── src/
│   └── run.js          # Function entry point
├── input.graphql       # Defines what data the function receives
├── shopify.extension.toml  # Extension configuration
└── package.json

Configuration (shopify.extension.toml)

api_version = "<latest-stable>"  # e.g. 2026-07 — Shopify ships a new stable version quarterly
#                                (2026-01 / 2026-04 / 2026-07 / 2026-10) and supports each for ~12 months.
#                                Resolve the current one from https://shopify.dev/docs/api/usage/versioning
#                                before writing this file. Never copy a version literal from an example.

[[extensions]]
name = "My Discount"
handle = "my-discount"
type = "function"

  [extensions.build]
  command = "npm exec -- shopify app function build"
  path = "dist/function.wasm"

  [extensions.ui]
  handle = "my-discount-ui"

  [extensions.input.variables]
  namespace = "my-app"
  key = "discount-config"

Input Query (input.graphql)

Defines what Shopify data the function receives:

query Input {
  cart {
    lines {
      id
      quantity
      merchandise {
        ... on ProductVariant {
          id
          product {
            id
            hasAnyTag(tags: ["vip"])
          }
        }
      }
      cost {
        amountPerQuantity {
          amount
          currencyCode
        }
      }
    }
  }
  discountNode {
    metafield(namespace: "my-app", key: "discount-config") {
      value
    }
  }
}

JavaScript Example

// src/run.js
export function run(input) {
  const config = JSON.parse(input.discountNode.metafield?.value || '{}');
  const percentage = config.percentage || 0;

  const targets = input.cart.lines
    .filter(line => line.merchandise?.product?.hasAnyTag)
    .map(line => ({
      productVariant: { id: line.merchandise.id }
    }));

  if (targets.length === 0) {
    return { discounts: [], discountApplicationStrategy: "FIRST" };
  }

  return {
    discounts: [{
      value: { percentage: { value: String(percentage) } },
      targets,
      message: `${percentage}% VIP discount`,
    }],
    discountApplicationStrategy: "FIRST",
  };
}

Performance Constraints

Best Practices

Fetch the Shopify Functions documentation for exact I/O schemas, supported function types, and API version requirements before implementing.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.