0

Add Products to Collection

by
Published Nov 8, 2023

Manually add products to a collection.

Script shopify Verified

The script

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

3
/**
4
 * Add Products to Collection
5
 * Manually add products (numeric IDs or GIDs) to a collection. The products are added as manual selections on the collection's own source, which is created if the collection has none yet.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  collection_id: string,
10
  product_ids: string[]
11
) {
12
  const id = collection_id.startsWith("gid://")
13
    ? collection_id
14
    : `gid://shopify/Collection/${collection_id}`
15
  const selections = product_ids.map((p) => ({
16
    productId: p.startsWith("gid://") ? p : `gid://shopify/Product/${p}`,
17
  }))
18
  const url = `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`
19
  const headers = {
20
    "X-Shopify-Access-Token": auth.token,
21
    "Content-Type": "application/json",
22
    Accept: "application/json",
23
  }
24

25
  // 1. Find the collection's own (non-shareable) conditions source
26
  const lookup = await fetch(url, {
27
    method: "POST",
28
    headers,
29
    body: JSON.stringify({
30
      query: `query ($id: ID!) { collection(id: $id) { title sources { id ... on CollectionConditionsSource { shareable } } } }`,
31
      variables: { id },
32
    }),
33
  })
34
  if (!lookup.ok) {
35
    throw new Error(`${lookup.status} ${await lookup.text()}`)
36
  }
37
  const found = await lookup.json()
38
  if (found.errors) {
39
    throw new Error(JSON.stringify(found.errors))
40
  }
41
  const collection = found.data.collection
42
  if (!collection) {
43
    throw new Error(`Collection ${id} not found`)
44
  }
45
  const source = collection.sources.find(
46
    (s: { shareable?: boolean }) => s.shareable === false
47
  )
48

49
  // 2. Add the products to that source, or create one holding them
50
  const response = await fetch(url, {
51
    method: "POST",
52
    headers,
53
    body: JSON.stringify({
54
      query: `mutation ($collection: CollectionUpdateInput!) { collectionUpdate(collection: $collection) { collection { id legacyResourceId title productsCount { count } sources { id title } } userErrors { field message } } }`,
55
      variables: {
56
        collection: source
57
          ? {
58
              id,
59
              sourcesToUpdate: [
60
                {
61
                  condition: {
62
                    id: source.id,
63
                    inclusion: { selectionsToAdd: selections },
64
                  },
65
                },
66
              ],
67
            }
68
          : {
69
              id,
70
              sourcesToCreate: [
71
                {
72
                  source: {
73
                    title: collection.title,
74
                    inclusion: { selections },
75
                  },
76
                },
77
              ],
78
            },
79
      },
80
    }),
81
  })
82

83
  if (!response.ok) {
84
    throw new Error(`${response.status} ${await response.text()}`)
85
  }
86

87
  const { data, errors } = await response.json()
88
  if (errors) {
89
    throw new Error(JSON.stringify(errors))
90
  }
91
  if (data.collectionUpdate.userErrors.length) {
92
    throw new Error(JSON.stringify(data.collectionUpdate.userErrors))
93
  }
94

95
  return data.collectionUpdate
96
}
97

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
     * Adds a product to a custom collection
    7
     * Adds a product to a custom collection.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      body: {
    13
        collect?: {
    14
          collection_id?: number;
    15
          product_id?: number;
    16
          [k: string]: unknown;
    17
        };
    18
        [k: string]: unknown;
    19
      }
    20
    ) {
    21
      const url = new URL(
    22
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/collects.json`
    23
      );
    24
    
    
    25
      const response = await fetch(url, {
    26
        method: "POST",
    27
        headers: {
    28
          "Content-Type": "application/json",
    29
          "X-Shopify-Access-Token": auth.token,
    30
        },
    31
        body: JSON.stringify(body),
    32
      });
    33
      if (!response.ok) {
    34
        const text = await response.text();
    35
        throw new Error(`${response.status} ${text}`);
    36
      }
    37
      return await response.json();
    38
    }
    39