Edits history of script submission #23737 for ' Update Order (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update Order
     * 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.
     */
    export async function main(
      auth: RT.Shopify,
      order_id: string,
      email: string | undefined,
      phone: string | undefined,
      note: string | undefined,
      tags: string[] | undefined,
      po_number: string | undefined,
      shipping_address:
        | {
            firstName?: string
            lastName?: string
            company?: string
            address1?: string
            address2?: string
            city?: string
            provinceCode?: string
            zip?: string
            countryCode?: string
            phone?: string
          }
        | undefined,
      custom_attributes: { key: string; value: string }[] | undefined
    ) {
      const id = order_id.startsWith("gid://")
        ? order_id
        : `gid://shopify/Order/${order_id}`
      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 ($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 } } }`,
            variables: {
              input: {
                id,
                email: email || undefined,
                phone: phone || undefined,
                note: note || undefined,
                tags: tags?.length ? tags : undefined,
                poNumber: po_number || undefined,
                shippingAddress:
                  shipping_address && Object.keys(shipping_address).length
                    ? shipping_address
                    : undefined,
                customAttributes: custom_attributes?.length
                  ? custom_attributes
                  : undefined,
              },
            },
          }),
        }
      )
    
      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.orderUpdate.userErrors.length) {
        throw new Error(JSON.stringify(data.orderUpdate.userErrors))
      }
    
      return data.orderUpdate
    }
    

    Submitted by hugo989 3 hours ago