Metafields API Guides

GraphQL Storefront API: Cart Metafields

Working with cart metafields

Cart metafields let you add custom metadata to a cart to display product information. Cart metafields are key-value pairs. You can work with them using both the GraphQL Storefront API and the REST Management API.

Example queries and mutations

Query cart metafields

Example query: Get cart metafields
query getCartMetafields {
site{
cart{
metafields(namespace: "bc_storefront"){
edges{
node{
id
key
value
}
}
}
}
}
}

Create a cart metafield

The platform limits are as follows:

  • 10 metafields per client (with customer ID or without ID) per cart
  • 250 metafields in total for a single cart (e.g., 25 clients * 10 metafields)
Example mutation: Create a cart metafield
mutation createCartMetafield {
cart {
createCartMetafield(input:{
cartEntityId: "4a5fd706-beb9-41b5-9ac2-01593b471168"
data: {
key: "size"
value: "small"
}
}) {
metafield {
id
entityId
key
value
}
errors {
...on Error {
message
}
}
}
}
}

Update a cart metafield

Example mutation: Update a cart metafield
1mutation updateCartMetafield {
2 cart {
3 updateCartMetafield(input:{
4 cartEntityId: "4a5fd706-beb9-41b5-9ac2-01593b471168"
5 metafieldEntityId:53
6 data: {
7 key: "Size",
8 value: "medium"
9 }
10 }) {
11 metafield {
12 id
13 entityId
14 key
15 value
16 }
17 errors {
18 ...on Error {
19 message
20 }
21 }
22 }
23}
24}

Delete a cart metafield

Example mutation: Delete a cart metafield
mutation deleteCartMetafield {
cart {
deleteCartMetafield(input:{
cartEntityId: "4a5fd706-beb9-41b5-9ac2-01593b471168"
metafieldEntityId: 53
}) {
errors {
... on NotFoundError {
__typename
message
}
... on ValidationError {
__typename
message
path
}
... on NotAuthorisedError {
__typename
}
}
}
}
}

Resources

GraphQL documentation