0

Update Order

by
Published Nov 8, 2023

Update an order's email, phone, note, tags, PO number, shipping address or custom attributes.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
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

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Updates an order
    7
     * Updates an order
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      order_id: string,
    13
      body: {
    14
        order?: {
    15
          buyer_accepts_marketing?: boolean;
    16
          id?: number;
    17
          [k: string]: unknown;
    18
        };
    19
        [k: string]: unknown;
    20
      }
    21
    ) {
    22
      const url = new URL(
    23
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/orders/${order_id}.json`
    24
      );
    25
    
    
    26
      const response = await fetch(url, {
    27
        method: "PUT",
    28
        headers: {
    29
          "Content-Type": "application/json",
    30
          "X-Shopify-Access-Token": auth.token,
    31
        },
    32
        body: JSON.stringify(body),
    33
      });
    34
      if (!response.ok) {
    35
        const text = await response.text();
    36
        throw new Error(`${response.status} ${text}`);
    37
      }
    38
      return await response.json();
    39
    }
    40