I’m having trouble accessing multiple metafields in Shopify discount functions. I used the **discountAutomaticAppCreate**
GraphQL mutation to create two automatic discounts with the same appId
and the same metafield namespace.
I’m currently creating the discount using the discountAutomaticAppCreate
mutation:
const client = new shopify.clients.Graphql({session});
const data = await client.query({
data: {
"query": `mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) {
discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) {
userErrors {
field
message
}
automaticAppDiscount {
discountId
title
startsAt
endsAt
status
appDiscountType {
appKey
functionId
}
combinesWith {
orderDiscounts
productDiscounts
shippingDiscounts
}
}
}
}`,
"variables": {
"automaticAppDiscount": {
"title": "Discount - 1",
"functionId": "abababab-abab-432f-43sdfd-98dd14fb8af5",
"startsAt": "2025-05-05T17:09:21Z",
"endsAt": "2025-05-05T17:09:21Z",
"combinesWith": {
"orderDiscounts": false,
"productDiscounts": false,
"shippingDiscounts": false
},
"metafields": [
{
"namespace": "test",
"key": "test-key-1",
"type": "json",
"value": "{\n \"discounts\": [{\n \"value\": {\"fixedAmount\": {\"amount\": 5}},\n \"targets\": [{\"orderSubtotal\": {\"excludedVariantIds\": []}}]\n }],\n \"discountApplicationStrategy\": \"FIRST\"\n}"
}
]
}
},
},
});
However, I’m unable to retrieve these metafield values within the Shopify discount functions. Below is the input.graphql code I’ve been working with.
Each discount has its own metafield data, but when I try to access them inside the Shopify Function, I can’t retrieve the values.
@tobebuilds suggested this approach, but unfortunately, it’s not working in my case.
query GetCartContents($selectedCollectionIds: [ID!]) {
cart {
lines {
quantity
merchandise {
__typename
... on ProductVariant {
id
product {
id
inAnyCollection(ids: $selectedCollectionIds)
}
}
}
}
}
discountNode {
metafield_1: metafield(namespace: "test", key: "test-key-1") {
value
}
metafield_2: metafield(namespace: "test", key: "test-key-2") {
value
}
}
}
Output I am getting:
"discountNode": {
"metafield1": null,
"metafield2": {
"value": "{\"title\":"Test metadata",\"products\":[]}"
},
},
I’d appreciate any insights or suggestions. Thanks in advance!