Edits history of script submission #23732 for ' Update Fulfillment Tracking Info (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update Fulfillment Tracking Info
     * Set the tracking number, URL and carrier of a fulfillment. Shopify replaces the tracking info with what you send, so pass every value you want to keep. notify_customer emails the customer the updated tracking.
     */
    export async function main(
      auth: RT.Shopify,
      fulfillment_id: string,
      tracking_number: string | undefined,
      tracking_url: string | undefined,
      tracking_company: string | undefined,
      notify_customer: boolean | undefined
    ) {
      if (!tracking_number && !tracking_url && !tracking_company) {
        throw new Error(
          "Pass at least one of tracking_number, tracking_url, tracking_company"
        )
      }
    
      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 ($fulfillmentId: ID!, $trackingInfoInput: FulfillmentTrackingInput!, $notifyCustomer: Boolean) { fulfillmentTrackingInfoUpdate(fulfillmentId: $fulfillmentId, trackingInfoInput: $trackingInfoInput, notifyCustomer: $notifyCustomer) { fulfillment { id name status displayStatus updatedAt trackingInfo { company number url } } userErrors { field message } } }`,
            variables: {
              fulfillmentId: fulfillment_id.startsWith("gid://")
                ? fulfillment_id
                : `gid://shopify/Fulfillment/${fulfillment_id}`,
              trackingInfoInput: {
                number: tracking_number || undefined,
                url: tracking_url || undefined,
                company: tracking_company || undefined,
              },
              notifyCustomer: notify_customer || 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.fulfillmentTrackingInfoUpdate.userErrors.length) {
        throw new Error(
          JSON.stringify(data.fulfillmentTrackingInfoUpdate.userErrors)
        )
      }
    
      return data.fulfillmentTrackingInfoUpdate
    }
    

    Submitted by hugo989 3 hours ago