Issue using variables in function input queries

Topic summary

A discount Function uses an input variable ($selectedCollectionIds) to check product membership via inAnyCollection, but results are always false. The metafield holding the IDs (namespace: discountApp, key: collectionData) was created on the Shop.

Key update: The issue is the metafield owner. For Function input variables, values must come from a metafield on the function owner (the discount), not the Shop. Change ownerId to the discount (function owner) when setting the metafield so the variable is populated in the input query.

Clarification: You can read Shop metafields in the input query (e.g., for other data), but you cannot use Shop metafields to supply input variables; variables must be on the function owner.

Open question: A participant asks how to manage shared, frequently changing logic across hundreds of discounts if variables must be per-discount, implying potential maintenance overhead. No resolution provided; guidance focuses on correcting the metafield owner to fix the immediate issue.

Summarized with AI on January 7. AI used: gpt-5.

I am developing a Shopify Function to apply discounts based on customer tags and product collection memberships.

collectionData (Namespace: discountApp): Holds only the variable selectedCollectionIds with an array of collection IDs.

          {
            "node": {
              "id": "gid://shopify/Metafield/27485936845038",
              "namespace": "discountApp",
              "key": "collectionData",
              "value": "{\"selectedCollectionIds\":[\"gid://shopify/Collection/422046531822\",\"gid://shopify/Collection/421527421166\"]}",
              "type": "json",
            }
          }

I have included this in my shopify.extension.toml file:

[extensions.input.variables]
  namespace = "discountApp"
  key = "collectionData"

and here is my run.graphql:

query Input($selectedCollectionIds: [ID!]) {
  cart {
    lines {
      merchandise {
        ... on ProductVariant {
          product {
            id
            handle
            inAnyCollection(ids: $selectedCollectionIds)
          }
        }
      }
    }
    buyerIdentity {
      customer {
        metafield(namespace: "discountApp", key: "tag") {
          value
        }
      }
    }
  }
  shop {
    metafield(namespace: "discountApp", key: "tags") {
      value
    }
  }
}

Here is what is returned to me from Shopify:

{
  "cart": {
    "lines": [
      {
        "merchandise": {
          "product": {
            "id": "gid://shopify/Product/8212928430318",
            "handle": "hat",
            "inAnyCollection": false
          }
        }
      },
      {
        "merchandise": {
          "product": {
            "id": "gid://shopify/Product/8208496492782",
            "handle": "t-shirt",
            "inAnyCollection": false
          }
        }
      }
    ],
    "buyerIdentity": {
      "customer": {
        "metafield": {
          "value": "Pro"
        }
      }
    }
  },
  "shop": {
    "metafield": {
      "value": "{\"Pro\":{\"collections\":{\"hats\":\"20%\",\"pants\":\"10%\",\"shirts\":\"35%\"}},\"VIP\":{\"collections\":{\"hats\":\"30%\",\"pants\":\"20%\",\"shirts\":\"40%\"}}}"
    }
  }
}

and here is the graphql code I used to create the metafield:

mutation {
  metafieldsSet(metafields: [
    {
      namespace: "discountApp",
      key: "collectionData",
      type: "json",
      value: "{\"selectedCollectionIds\":[\"gid://shopify/Collection/422046531822\",\"gid://shopify/Collection/421527421166\"]}",
      ownerId: "gid://shopify/Shop/61637230830"
    }
  ]) {
    metafields {
      id
    }
  }
}

But no matter what I do, I cannot get inAnyCollection to be true. Any suggestions or help?

This is your issue here:

ownerId: "gid://shopify/Shop/61637230830"

As mentioned in input query docs, the variable values need to be set on a metafield on the function owner, which is your case is probably the discount, based on your metafield naming.

1 Like

Does this mean that you can read shop metafields in the input query, but you can’t use shop metafields for the input variables?

If so, this is a significant limitation. What do you do if you have several hundred discount codes that all follow the same logic, which changes on a weekly basis?