Edits history of script submission #23715 for ' Get Draft Order (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get Draft Order
     * Get a draft order with its customer, addresses, discount, shipping line, totals, invoice URL and first 50 line items. Once completed, order holds the resulting order.
     */
    export async function main(auth: RT.Shopify, draft_order_id: string) {
      const id = draft_order_id.startsWith("gid://")
        ? draft_order_id
        : `gid://shopify/DraftOrder/${draft_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: `query ($id: ID!) { draftOrder(id: $id) { id legacyResourceId name status email phone createdAt updatedAt completedAt invoiceUrl invoiceSentAt tags note2 ready reserveInventoryUntil subtotalPriceSet { shopMoney { amount currencyCode } } totalDiscountsSet { shopMoney { amount currencyCode } } totalShippingPriceSet { shopMoney { amount currencyCode } } totalTaxSet { shopMoney { amount currencyCode } } totalPriceSet { shopMoney { amount currencyCode } } customer { id displayName defaultEmailAddress { emailAddress } } order { id name } customAttributes { key value } appliedDiscount { title description value valueType amountSet { shopMoney { amount currencyCode } } } shippingLine { title originalPriceSet { shopMoney { amount currencyCode } } } shippingAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } billingAddress { name firstName lastName company address1 address2 city provinceCode zip countryCodeV2 phone } lineItems(first: 50) { nodes { id name sku quantity custom originalUnitPriceSet { shopMoney { amount currencyCode } } discountedTotalSet { shopMoney { amount currencyCode } } variant { id title } product { id title } } pageInfo { hasNextPage endCursor } } } }`,
            variables: { id },
          }),
        }
      )
    
      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))
      }
    
      return data.draftOrder
    }
    

    Submitted by hugo989 3 hours ago