0

Update Product

by
Published Nov 8, 2023

Update a product's title, description, vendor, type, tags, status, handle or SEO.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Update Product
5
 * Update a product's fields. Only the fields you fill in are changed; tags, when given, replace all existing tags (use Add Tags / Remove Tags to edit them incrementally).
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  product_id: string,
10
  title: string | undefined,
11
  description_html: string | undefined,
12
  vendor: string | undefined,
13
  product_type: string | undefined,
14
  tags: string[] | undefined,
15
  status: "DRAFT" | "ACTIVE" | "UNLISTED" | "ARCHIVED" | undefined,
16
  handle: string | undefined,
17
  seo_title: string | undefined,
18
  seo_description: string | undefined
19
) {
20
  const id = product_id.startsWith("gid://")
21
    ? product_id
22
    : `gid://shopify/Product/${product_id}`
23

24
  const response = await fetch(
25
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
26
    {
27
      method: "POST",
28
      headers: {
29
        "X-Shopify-Access-Token": auth.token,
30
        "Content-Type": "application/json",
31
        Accept: "application/json",
32
      },
33
      body: JSON.stringify({
34
        query: `mutation ($product: ProductUpdateInput!) { productUpdate(product: $product) { product { id legacyResourceId title handle status vendor productType tags descriptionHtml updatedAt seo { title description } } userErrors { field message } } }`,
35
        variables: {
36
          product: {
37
            id,
38
            title: title || undefined,
39
            descriptionHtml: description_html || undefined,
40
            vendor: vendor || undefined,
41
            productType: product_type || undefined,
42
            tags: tags?.length ? tags : undefined,
43
            status: status || undefined,
44
            handle: handle || undefined,
45
            seo:
46
              seo_title || seo_description
47
                ? {
48
                    title: seo_title || undefined,
49
                    description: seo_description || undefined,
50
                  }
51
                : undefined,
52
          },
53
        },
54
      }),
55
    }
56
  )
57

58
  if (!response.ok) {
59
    throw new Error(`${response.status} ${await response.text()}`)
60
  }
61

62
  const { data, errors } = await response.json()
63
  if (errors) {
64
    throw new Error(JSON.stringify(errors))
65
  }
66
  if (data.productUpdate.userErrors.length) {
67
    throw new Error(JSON.stringify(data.productUpdate.userErrors))
68
  }
69

70
  return data.productUpdate
71
}
72

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Updates a product
    7
     * Updates a product and its variants and images. If you want to update the product's SEO information, then you can use the following properties:  metafields_global_title_tag: The name of the product used for SEO purposes. Generally added to the <meta name='title'> tag. metafields_global_description_tag: A description of the product used for SEO purposes. Generally added to the <meta name='description'> tag.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      product_id: string,
    13
      body: {
    14
        product?: { id?: number; tags?: string; [k: string]: unknown };
    15
        [k: string]: unknown;
    16
      }
    17
    ) {
    18
      const url = new URL(
    19
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/products/${product_id}.json`
    20
      );
    21
    
    
    22
      const response = await fetch(url, {
    23
        method: "PUT",
    24
        headers: {
    25
          "Content-Type": "application/json",
    26
          "X-Shopify-Access-Token": auth.token,
    27
        },
    28
        body: JSON.stringify(body),
    29
      });
    30
      if (!response.ok) {
    31
        const text = await response.text();
    32
        throw new Error(`${response.status} ${text}`);
    33
      }
    34
      return await response.json();
    35
    }
    36