0

Cancel Fulfillment Order

by
Published Nov 8, 2023

Cancel a fulfillment order and get a replacement for the remaining work.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Cancel Fulfillment Order
5
 * Cancel a fulfillment order: the original is closed and a new replacementFulfillmentOrder holds the unfulfilled work, e.g. to reassign it. Works on unsubmitted fulfillment orders and, for a fulfillment service, while the request is SUBMITTED or CANCELLATION_REQUESTED; once the service has accepted it, use Submit Cancellation Request instead.
6
 */
7
export async function main(auth: RT.Shopify, fulfillment_order_id: string) {
8
  const response = await fetch(
9
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
10
    {
11
      method: "POST",
12
      headers: {
13
        "X-Shopify-Access-Token": auth.token,
14
        "Content-Type": "application/json",
15
        Accept: "application/json",
16
      },
17
      body: JSON.stringify({
18
        query: `mutation ($id: ID!) { fulfillmentOrderCancel(id: $id) { fulfillmentOrder { id status requestStatus } replacementFulfillmentOrder { id status requestStatus } userErrors { field message } } }`,
19
        variables: {
20
          id: fulfillment_order_id.startsWith("gid://")
21
            ? fulfillment_order_id
22
            : `gid://shopify/FulfillmentOrder/${fulfillment_order_id}`,
23
        },
24
      }),
25
    }
26
  )
27

28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${await response.text()}`)
30
  }
31

32
  const { data, errors } = await response.json()
33
  if (errors) {
34
    throw new Error(JSON.stringify(errors))
35
  }
36

37
  if (data.fulfillmentOrderCancel.userErrors.length) {
38
    throw new Error(JSON.stringify(data.fulfillmentOrderCancel.userErrors))
39
  }
40

41
  return data.fulfillmentOrderCancel
42
}
43

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
     * Cancel a fulfillment order
    7
     * Marks a fulfillment order as cancelled.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      fulfillment_order_id: string,
    13
      body: {
    14
        fulfillment_order?: {
    15
          assigned_location?: {
    16
            address1?: { [k: string]: unknown };
    17
            address2?: { [k: string]: unknown };
    18
            city?: { [k: string]: unknown };
    19
            country_code?: string;
    20
            location_id?: number;
    21
            name?: string;
    22
            phone?: { [k: string]: unknown };
    23
            province?: { [k: string]: unknown };
    24
            zip?: { [k: string]: unknown };
    25
            [k: string]: unknown;
    26
          };
    27
          assigned_location_id?: number;
    28
          destination?: {
    29
            address1?: string;
    30
            address2?: string;
    31
            city?: string;
    32
            company?: { [k: string]: unknown };
    33
            country?: string;
    34
            email?: string;
    35
            first_name?: string;
    36
            id?: number;
    37
            last_name?: string;
    38
            phone?: string;
    39
            province?: string;
    40
            zip?: string;
    41
            [k: string]: unknown;
    42
          };
    43
          fulfillment_service_handle?: string;
    44
          id?: number;
    45
          line_items?: {
    46
            fulfillable_quantity?: number;
    47
            fulfillment_order_id?: number;
    48
            id?: number;
    49
            inventory_item_id?: number;
    50
            line_item_id?: number;
    51
            quantity?: number;
    52
            shop_id?: number;
    53
            variant_id?: number;
    54
            [k: string]: unknown;
    55
          }[];
    56
          merchant_requests?: unknown[];
    57
          order_id?: number;
    58
          request_status?: string;
    59
          shop_id?: number;
    60
          status?: string;
    61
          supported_actions?: string[];
    62
          [k: string]: unknown;
    63
        };
    64
        [k: string]: unknown;
    65
      }
    66
    ) {
    67
      const url = new URL(
    68
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/fulfillment_orders/${fulfillment_order_id}/cancel.json`
    69
      );
    70
    
    
    71
      const response = await fetch(url, {
    72
        method: "POST",
    73
        headers: {
    74
          "Content-Type": "application/json",
    75
          "X-Shopify-Access-Token": auth.token,
    76
        },
    77
        body: JSON.stringify(body),
    78
      });
    79
      if (!response.ok) {
    80
        const text = await response.text();
    81
        throw new Error(`${response.status} ${text}`);
    82
      }
    83
      return await response.json();
    84
    }
    85