cartApplyCode
Overview
Apply a coupon/promo code to the current cart. Provide the cart ID (or token) and the desired code; the response echoes the updated cart data.
- Type: GraphQL Mutation
- Endpoint: https://appssubdomain.com/api/graphql
- Authentication:
Authorization: Bearer <TOKEN>
Mutation
mutation ApplyCode($cart: String!, $code: String!) {
applyCode(cart: $cart, code: $code) {
data {
_id
app
account {
_id
app
verified
blocked
orderCount
lastSeen
createdAt
updatedAt
}
coupon
kind
couponApplied
couponDiscount
items {
productId
_id
variantId
productData {
title
slug
image { _id fileUrl }
price
}
variantData {
price
compareAtPrice
options {
_id
option { _id name type product }
label
sortOrder
}
}
quantity
price
compareAtPrice
totalPrice
totalCompareAtPrice
totalSavings
}
deviceId
sessionId
status
totalQuantity
totalPrice
totalCompareAtPrice
totalSavings
isFastOrder
createdAt
updatedAt
url
encryptionKey
}
success
message
}
}
Variables
{
"cart": "<CART_ID_OR_TOKEN>",
"code": "<COUPON_CODE>"
}
Examples
- cURL
- JavaScript (fetch)
curl -X POST \
https://appssubdomain.com/api/graphql \
-H "Content-Type": "application/json" \
-H "Authorization: Bearer <TOKEN>" \
--data-binary @- << 'EOF'
{
"query": "mutation ApplyCode($cart: String!, $code: String!) { applyCode(cart: $cart, code: $code) { success message data { _id coupon couponApplied totalPrice } } }",
"variables": { "cart": "<CART_ID>", "code": "<COUPON_CODE>" }
}
EOF
const res = await fetch('https://appssubdomain.com/api/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
},
body: JSON.stringify({
query: `
mutation ApplyCode($cart: String!, $code: String!) {
applyCode(cart: $cart, code: $code) {
success
message
data { _id coupon couponApplied totalPrice }
}
}
`,
variables: { cart: '<CART_ID>', code: '<COUPON_CODE>' },
}),
});
const json = await res.json();
Notes
- Ensure
cartmatches how your system identifies carts (ID, token, session). - Handle
success/messageto surface invalid/expired code errors to shoppers. - After applying a code successfully, re-render totals and item pricing using the returned
data.