Discussing APIs and development related to customers, discounts, and order management.
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.
The reason I need this is because:
If possible, Please share the GraphQL request to add the codes. Thanks!
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
}
}
}
}
}
}
}
}
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
}
}
}
}
}
}
}
}
I had the same problem and realized that the REST api will let you create multiple codes using a price rule. So first, create a price rule. Then, you can create a batch of discount codes from the rule.
https://shopify.dev/api/admin-rest/2022-04/resources/pricerule#post-price-rules
https://shopify.dev/api/admin-rest/2022-04/resources/discountcode#post-price-rules-price-rule-id-bat...
The REST API does work but is missing key properties that the new GQL Discount Code type offers. It seems like Shopify is moving away from REST in general and using GQL; however the PriceRule in the GQL API is being deprecated.
From what I can see from the docs, there is no way to tie in more than one code under one ruleset; however, this should be supported.
There's not even a way of doing this through the UI..