How can I get multiple mefields or external datas in Discount functions?

Topic summary

Main issue: Accessing multiple metafields or external data within Shopify discount functions to validate cart lines.

  • Current capability: The function can query cart data and the discount’s own metafield (via discountNode.metafield) using GraphQL. A code snippet fetching cart lines and a specific discount metafield is central to the discussion.

  • Question: How to pull additional metafields or external inputs for dynamic conditions.

  • Resolution/Update: Only cart data and discount node values are available inside discount functions. External or dynamic data must be supplied via supported metafields. Shopify’s documentation (linked) outlines the metafield input objects available to product discount functions.

  • Practical outcome: To implement dynamic validation, store needed conditions as metafields that the function can read per the allowed schema rather than trying to fetch arbitrary external data.

  • Definitions: Metafields are custom key–value fields used to store extra data that functions can consume.

Status: Resolved by the original poster with a documentation reference; no open questions remain.

Summarized with AI on December 13. AI used: gpt-5.

Hi everyone,

I need to get the external inputs in discount functions. I can get the discount metafield in discount functions for that particular discount. Even though I need some dynamic data, It’s like some conditions to validate cart lines to apply discount. How can I get some other metafield data in discount functions? Is there any way to get some other data in the function?

query RunInput($selectedCollectionIds: [ID!]) {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ... on ProductVariant {
          id
          product {
            id
            inAnyCollection(ids: $selectedCollectionIds)
          }
        }
      }
    }
  }
  discountNode {
    metafield(
      key: "test-function-config"
      namespace: "test-discount"
    ) {
      value
    }
  }
}

It’s how I can get the discount metafield. How can I access some other metafield?

Hi Everyone,

We can only obtain cart data and discount node values inside the discount functions. For external data, we should use metafields. Here are the details of the metafield inputs that can be utilized in discount functions. https://shopify.dev/docs/api/functions/reference/product-discounts/graphql/common-objects/metafield