Anchor to collectionAddProductscollection
collectionAddProducts
mutation
Requires access scope. Also: The store must not be on the Starter or Retail plans and user must have a permission to add products to a collection.
Adds products to a collection.
Anchor to Arguments
Arguments
- •ID!required
The ID of the collection that's being updated. This can't be a smart collection.
- Anchor to productIdsproduct•
Ids [ID!]!required The IDs of the products that are being added to the collection. If any of the products is already present in the input collection, then an error is raised and no products are added.
Was this section helpful?
Anchor to CollectionAddProductsPayload returnsCollectionAddProductsPayload returns
- Anchor to collectioncollection•
The updated collection. Returns
null
if an error is raised.- Anchor to userErrorsuser•
Errors [UserError!]! non-null The list of errors that occurred from executing the mutation.
Was this section helpful?
- Add a product to a collection that doesn't exist
- Add a product to a smart collection
- Add products that already belong to the specified collection
- Add products that don't exist to an existing collection
- Add products to an existing collection
- Add products to collections will fail when CollectionsAccess returns false
- Adds a product to a custom collection
- collectionAddProducts reference
Examples
1const { admin } = await authenticate.admin(request);23const response = await admin.graphql(4 `#graphql5 mutation collectionAddProducts($id: ID!, $productIds: [ID!]!) {6 collectionAddProducts(id: $id, productIds: $productIds) {7 collection {8 id9 title10 products(first: 10) {11 nodes {12 id13 title14 }15 }16 }17 userErrors {18 field19 message20 }21 }22 }`,23 {24 variables: {25 "id": "gid://shopify/Collection/-1",26 "productIds": [27 "gid://shopify/Product/108828309"28 ]29 },30 },31);3233const data = await response.json();34
mutation collectionAddProducts($id: ID!, $productIds: [ID!]!) {
collectionAddProducts(id: $id, productIds: $productIds) {
collection {
id
title
products(first: 10) {
nodes {
id
title
}
}
}
userErrors {
field
message
}
}
}
curl -X POST \
https://your-development-store.myshopify.com/admin/api/2024-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation collectionAddProducts($id: ID!, $productIds: [ID!]!) { collectionAddProducts(id: $id, productIds: $productIds) { collection { id title products(first: 10) { nodes { id title } } } userErrors { field message } } }",
"variables": {
"id": "gid://shopify/Collection/-1",
"productIds": [
"gid://shopify/Product/108828309"
]
}
}'
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation collectionAddProducts($id: ID!, $productIds: [ID!]!) {
collectionAddProducts(id: $id, productIds: $productIds) {
collection {
id
title
products(first: 10) {
nodes {
id
title
}
}
}
userErrors {
field
message
}
}
}`,
{
variables: {
"id": "gid://shopify/Collection/-1",
"productIds": [
"gid://shopify/Product/108828309"
]
},
},
);
const data = await response.json();
const client = new shopify.clients.Graphql({session});
const data = await client.query({
data: {
"query": `mutation collectionAddProducts($id: ID!, $productIds: [ID!]!) {
collectionAddProducts(id: $id, productIds: $productIds) {
collection {
id
title
products(first: 10) {
nodes {
id
title
}
}
}
userErrors {
field
message
}
}
}`,
"variables": {
"id": "gid://shopify/Collection/-1",
"productIds": [
"gid://shopify/Product/108828309"
]
},
},
});
session = ShopifyAPI::Auth::Session.new(
shop: "your-development-store.myshopify.com",
access_token: access_token
)
client = ShopifyAPI::Clients::Graphql::Admin.new(
session: session
)
query = <<~QUERY
mutation collectionAddProducts($id: ID!, $productIds: [ID!]!) {
collectionAddProducts(id: $id, productIds: $productIds) {
collection {
id
title
products(first: 10) {
nodes {
id
title
}
}
}
userErrors {
field
message
}
}
}
QUERY
variables = {
"id": "gid://shopify/Collection/-1",
"productIds": ["gid://shopify/Product/108828309"]
}
response = client.query(query: query, variables: variables)
Input variables
JSON1{2 "id": "gid://shopify/Collection/-1",3 "productIds": [4 "gid://shopify/Product/108828309"5 ]6}
Response
JSON1{2 "collectionAddProducts": {3 "collection": null,4 "userErrors": [5 {6 "field": [7 "id"8 ],9 "message": "Collection does not exist"10 }11 ]12 }13}