How to make the 'hasTags' part of a discount function dynamic in a Shopify app?

Topic summary

A developer is building a Shopify app with a discount function and wants to make product and customer tags dynamic instead of hardcoded. Currently, the tags “productDiscount” and “customerDiscount” are static in the function code.

Solution provided involves three main steps:

  1. Define variables in run.graphql - Import dynamic tags as GraphQL variables (e.g., $customer_tag) and reference them in hasTags queries instead of hardcoded values

  2. Configure extension.toml - Declare the namespace and key for the metafield variables in the extensions configuration file

  3. Create metafields via admin mutation - When creating the discount through the admin API, pass the product and customer tag values as metafields using the same namespace

Important considerations:

  • Ensure the namespace used in the GraphQL variable input matches exactly what’s defined when creating the metafield
  • Test using shopify app dev, but full deployment via shopify app deploy is required for complete testing
  • Run shopify app function schema and typegen to finalize the setup
  • Check extension STDOUT in the partner dashboard for debugging

A link to official Shopify documentation on function variables and queries was also shared for reference.

Summarized with AI on November 5. AI used: claude-sonnet-4-5-20250929.

The documentation might seem a bit complex, so here’s a streamlined version:

First, define the namespace and key in your run.graphql file like this:

metafield(namespace: "custom", key: "discount_value")

Next in the extensions toml file you need to identify it:

[extensions.input.variables]
  namespace = "

Lastly, when creating the actual meta tag for what ever route/mutation you use in the admin you will push the **Product** & **Customer** tag using the name space:

```markup
const response = await admin.graphql(
    `#graphql
        mutation CreateAutomaticDiscount($discount: DiscountAutomaticAppInput!) {
          discountCreate: discountAutomaticAppCreate(automaticAppDiscount: $discount) {
            automaticAppDiscount {
              discountId
            }
            userErrors {
              code
              message
              field
            }
          }
        }`,
    {
      variables: {
        discount: {
          ...baseDiscount,
          metafields: [
            {
              namespace: "

Now in your Graphql file you can import the dynamic variable (tags for product/customer) like you would a graphql variable.

```markup
query RunInput($customer_tag: String! = "default-tag") {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ... on ProductVariant {
          id
        }
      }
    }
    buyerIdentity {
      customer {
        hasTags(tags: $customer_tag) {
          tag
          hasTag
        }
      }
    }
  }
  discountNode {
    metafield(namespace: "

Here I named the graphql input variable **customer_tag**. Be sure you match the grapql variable input with the one you called in the name space when making the metafeild (as shown when creating the discount). That is crucial.

Now you are ready.

1. Finalize by running shopify app function schema and **shopify app function schema** and shopify app function typegen. Next you can run s**hopify app dev** but at times that isn't sufficient and you will need to s**hopify app** **deploy** prior to testing. Lastly, you can test the functionality and check the extensions STDOUT in the partner dashboard within your app.