1 | |
2 |
|
3 | |
4 | * Update Order |
5 | * Update an order's email, phone, note, tags, PO number, shipping address or custom attributes. Only the fields you fill are changed; tags and custom_attributes replace the existing lists. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Shopify, |
9 | order_id: string, |
10 | email: string | undefined, |
11 | phone: string | undefined, |
12 | note: string | undefined, |
13 | tags: string[] | undefined, |
14 | po_number: string | undefined, |
15 | shipping_address: |
16 | | { |
17 | firstName?: string |
18 | lastName?: string |
19 | company?: string |
20 | address1?: string |
21 | address2?: string |
22 | city?: string |
23 | provinceCode?: string |
24 | zip?: string |
25 | countryCode?: string |
26 | phone?: string |
27 | } |
28 | | undefined, |
29 | custom_attributes: { key: string; value: string }[] | undefined |
30 | ) { |
31 | const id = order_id.startsWith("gid://") |
32 | ? order_id |
33 | : `gid://shopify/Order/${order_id}` |
34 | const response = await fetch( |
35 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
36 | { |
37 | method: "POST", |
38 | headers: { |
39 | "X-Shopify-Access-Token": auth.token, |
40 | "Content-Type": "application/json", |
41 | Accept: "application/json", |
42 | }, |
43 | body: JSON.stringify({ |
44 | query: `mutation ($input: OrderInput!) { orderUpdate(input: $input) { order { id legacyResourceId name email phone note tags poNumber customAttributes { key value } shippingAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } updatedAt } userErrors { field message } } }`, |
45 | variables: { |
46 | input: { |
47 | id, |
48 | email: email || undefined, |
49 | phone: phone || undefined, |
50 | note: note || undefined, |
51 | tags: tags?.length ? tags : undefined, |
52 | poNumber: po_number || undefined, |
53 | shippingAddress: |
54 | shipping_address && Object.keys(shipping_address).length |
55 | ? shipping_address |
56 | : undefined, |
57 | customAttributes: custom_attributes?.length |
58 | ? custom_attributes |
59 | : undefined, |
60 | }, |
61 | }, |
62 | }), |
63 | } |
64 | ) |
65 |
|
66 | if (!response.ok) { |
67 | throw new Error(`${response.status} ${await response.text()}`) |
68 | } |
69 |
|
70 | const { data, errors } = await response.json() |
71 | if (errors) { |
72 | throw new Error(JSON.stringify(errors)) |
73 | } |
74 |
|
75 | if (data.orderUpdate.userErrors.length) { |
76 | throw new Error(JSON.stringify(data.orderUpdate.userErrors)) |
77 | } |
78 |
|
79 | return data.orderUpdate |
80 | } |
81 |
|