Hey All,
I am trying to create discount codes programmatically in a shopify store, using the GraphQL requests. The requests are successful & I can see the codes being generated in the store. I have provided the screenshots below. The GraphQL requests I am using is also shared below.
As you can see in the Screenshot 3, for every request I make a single discount code “12345678” gets created. Lets say under “code basic test”, I want to add multiple unique codes like “12345678”, “abcd”, “wxyz” etc.; is it doable? This way I can generate many unique codes where all codes will follow the “code basic test” discount rules.
- Is there any way to create multiple codes under the “code basic test” container?
- I would like to generate these codes on an ongoing basis i.e. would prefer to add codes on a daily basis rather than to create all codes one time. I tried using the update mutation (request payload shared below) & what it does is, it overwrites the existing code with a new code rather than adding a new code.
The reason I need this is because:
- I need to create unique bulk codes. If I create a new “code basic test” discount code rule every time I generate a code, there will be 100s of records in the Shopify Discounts section. This will not be a good experience.
- Plus, the maintenance of codes will be difficult. I would rather prefer a single record which I can modify anytime & all the associated codes “12345678”, “abcd”, “wxyz” etc. will get updated.
If possible, Please share the GraphQL request to add the codes. Thanks!
Newly Created Code in Discounts Section### Screenshot 1
Screenshot 2
Screenshot 3
Create Code GraphQL Request
mutation {
discountCodeBasicCreate(basicCodeDiscount: {
title: "code basic test",
startsAt: "2019-01-01",
endsAt: "2023-01-01",
customerSelection: {
all: true
}
code: "12345678",
customerGets: {
value: {
discountAmount: {
amount: 1.00,
appliesOnEachItem: false
}
}
items: {
all: true
}
}}) {
userErrors { field message code }
codeDiscountNode {
id
codeDiscount {
... on DiscountCodeBasic {
title
summary
status
codes (first:10) {
edges {
node {
code
}
}
}
}
}
}
}
}
Update GraphQL Request
mutation {
discountCodeBasicUpdate(id: "gid://shopify/DiscountCodeNode/1167105949949",
basicCodeDiscount: {
code: "xyzt5644261"
}) {
userErrors { field message code }
codeDiscountNode {
id
codeDiscount {
... on DiscountCodeBasic {
shareableUrls {
targetType,
title,
url
}
title
summary
status
codes (first:100) {
edges {
node {
code
}
}
}
}
}
}
}
}


