Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
I'm trying to use the GQL to create at discount code for a specific cusotmer. I was succesful in using the provided template code in the link below to add a discount code for all customers. But when I modify it I get a "Invalid global id" Error.
Documentation:
https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/discountCodeBasicCreate
Here are my variables
This is the customer id I'm using to test.
I'm using the provided template mutation code from the documentation:
query = '''
mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBasic {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
customerGets {
value {
... on DiscountPercentage {
percentage
}
}
items {
... on AllDiscountItems {
allItems
}
}
}
appliesOncePerCustomer
}
}
}
userErrors {
field
code
message
}
}
}
'''
Error response:
{"errors":[{"message":"Variable $basicCodeDiscount of type DiscountCodeBasicInput! was provided invalid value for customerSelection.customers.add.0 (Invalid global id '7929561841982')","locations":[{"line":2,"column":34}],"extensions":{"value":{"title":"test3","code":"test3","startsAt":"2022-06-21T00:00:00Z","endsAt":"2022-09-21T00:00:00Z","customerSelection":{"all":false,"customers":{"add":[7929561841982]}},"customerGets":{"value":{"percentage":0.2},"items":{"all":true}},"appliesOncePerCustomer":true},"problems":[{"path":["customerSelection","customers","add",0],"explanation":"Invalid global id '7929561841982'","message":"Invalid global id '7929561841982'"}]}}]}
I finally got it to work. The fix was that the customer input was not correct it should be formated like this:
"customerSelection": {
'customers': {
'add': ['gid://shopify/Customer/7936674496830']
}
},
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
... on DiscountCustomers {
customers {
id
firstName
lastName
email
}
}
}
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024