Edits history of script submission #23727 for ' Update Customer (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update Customer
     * Update a customer's email, phone, name, note or tags. Only the fields you fill are changed; tags replace the existing list. Use the address actions to change addresses.
     */
    export async function main(
      auth: RT.Shopify,
      customer_id: string,
      email: string | undefined,
      phone: string | undefined,
      first_name: string | undefined,
      last_name: string | undefined,
      note: string | undefined,
      tags: string[] | undefined
    ) {
      const id = customer_id.startsWith("gid://")
        ? customer_id
        : `gid://shopify/Customer/${customer_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: CustomerInput!) { customerUpdate(input: $input) { customer { id legacyResourceId displayName firstName lastName state tags note locale verifiedEmail taxExempt numberOfOrders amountSpent { amount currencyCode } createdAt updatedAt defaultEmailAddress { emailAddress marketingState marketingOptInLevel } defaultPhoneNumber { phoneNumber } defaultAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } } userErrors { field message } } }`,
            variables: {
              input: {
                id,
                email: email || undefined,
                phone: phone || undefined,
                firstName: first_name || undefined,
                lastName: last_name || undefined,
                note: note || undefined,
                tags: tags?.length ? tags : 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.customerUpdate.userErrors.length) {
        throw new Error(JSON.stringify(data.customerUpdate.userErrors))
      }
    
      return data.customerUpdate
    }
    

    Submitted by hugo989 3 hours ago