Edits history of script submission #23771 for ' Create Return (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Return
     * Open a return on a fulfilled order, already approved (unlike a customer return request). return_line_items take the fulfillment line item ids from the order's fulfillments (Get Fulfillment), a quantity and a return reason definition id (default 1 Unknown; 2 Changed my mind, 3 Not as described, 4 Wrong item, 5 Other, 6 Damaged or defective, 7 Too small, 8 Too big; full library via the returnReasonDefinitions query). Returns the return with its line item ids, to pass to Process Return. Needs the write_returns scope.
     */
    export async function main(
      auth: RT.Shopify,
      order_id: string,
      return_line_items: {
        fulfillment_line_item_id: string
        quantity: number
        return_reason_definition_id?: string
        return_reason_note?: string
      }[]
    ) {
      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 ($returnInput: ReturnInput!) { returnCreate(returnInput: $returnInput) { return { id name status createdAt totalQuantity order { id name } returnLineItems(first: 50) { nodes { id quantity returnReasonNote ... on ReturnLineItem { fulfillmentLineItem { id lineItem { id name } } } } } } userErrors { field message code } } }`,
            variables: {
              returnInput: {
                orderId: order_id.startsWith("gid://")
                  ? order_id
                  : `gid://shopify/Order/${order_id}`,
                returnLineItems: return_line_items.map((li) => ({
                  fulfillmentLineItemId: li.fulfillment_line_item_id.startsWith(
                    "gid://"
                  )
                    ? li.fulfillment_line_item_id
                    : `gid://shopify/FulfillmentLineItem/${li.fulfillment_line_item_id}`,
                  quantity: li.quantity,
                  returnReasonDefinitionId: `gid://shopify/ReturnReasonDefinition/${(li.return_reason_definition_id || "1").replace("gid://shopify/ReturnReasonDefinition/", "")}`,
                  returnReasonNote: li.return_reason_note || 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.returnCreate.userErrors.length) {
        throw new Error(JSON.stringify(data.returnCreate.userErrors))
      }
    
      return data.returnCreate
    }
    

    Submitted by hugo989 3 hours ago