//native
/**
* Update Discount Code
* Update a basic code discount (numeric ID or DiscountCodeNode GID). Only the fields you fill are changed; value needs value_type. Changing code renames the discount's code. Needs the write_discounts scope.
*/
export async function main(
auth: RT.Shopify,
discount_id: string,
title: string | undefined,
code: string | undefined,
value_type: "percentage" | "fixed_amount" | undefined,
value: number | undefined,
starts_at: string | undefined,
ends_at: string | undefined,
usage_limit: number | undefined,
applies_once_per_customer: "keep" | "true" | "false" | undefined,
minimum_subtotal: number | undefined
) {
const id = discount_id.startsWith("gid://")
? discount_id
: `gid://shopify/DiscountCodeNode/${discount_id}`
const input: { [key: string]: any } = {
title: title || undefined,
code: code || undefined,
startsAt: starts_at || undefined,
endsAt: ends_at || undefined,
usageLimit: usage_limit ?? undefined,
appliesOncePerCustomer:
applies_once_per_customer === "true"
? true
: applies_once_per_customer === "false"
? false
: undefined,
minimumRequirement:
minimum_subtotal != null
? {
subtotal: {
greaterThanOrEqualToSubtotal: String(minimum_subtotal),
},
}
: undefined,
}
if (value != null && value_type) {
input.customerGets = {
value:
value_type === "percentage"
? { percentage: value / 100 }
: {
discountAmount: {
amount: String(value),
appliesOnEachItem: false,
},
},
}
}
const response = await fetch(
`https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
{
method: "POST",
headers: {
"X-Shopify-Access-Token": auth.token,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
query: `mutation ($id: ID!, $input: DiscountCodeBasicInput!) { discountCodeBasicUpdate(id: $id, basicCodeDiscount: $input) { codeDiscountNode { id codeDiscount { ... on DiscountCodeBasic { title status summary startsAt endsAt usageLimit appliesOncePerCustomer updatedAt codes(first: 10) { nodes { id code } } } } } userErrors { field message } } }`,
variables: { id, input },
}),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { data, errors } = await response.json()
if (errors) {
throw new Error(JSON.stringify(errors))
}
if (data.discountCodeBasicUpdate.userErrors.length) {
throw new Error(JSON.stringify(data.discountCodeBasicUpdate.userErrors))
}
return data.discountCodeBasicUpdate
}
Submitted by hugo989 3 hours ago