Applying a discount
NOTE: Only one discount can be applied to an order.
A discount can be applied to a cart in one of two ways:
Automatic Discounts
Automatic discounts are applied at checkout without the customer needing to take any action.
To check if a discount exists simply run this mutation:
mutation applyAutomaticDiscount($cartId: ID!) {
discount: applyAutomaticDiscount(cartId: $cartId) {
discountCode
totalDiscount
discountId
value
target
trigger
type
}
}
If no discounts exist to apply this error will return:
{
"data": {
"discount": null
},
"errors": [
{
"locations": [
{
"column": 0,
"line": 2
}
],
"message": "No eligible automatic discounts for cart",
"path": [
"discount"
]
}
]
}
If an eligible discount is found it will return the discount details:
{
"data": {
"discount": {
"discountCode": "XXXXXXXXXX",
"discountId": "92a7600b-bb22-4b24-8cdb-a71f02d50007",
"target": "all_products",
"totalDiscount": "6.75",
"trigger": "automatic",
"type": "percentage",
"value": "50"
}
}
}
And these will be applied to the cart
Manual Discounts
Manual discounts are discounts driven by a code entered on checkout.
If a customer enters a code you can check and apply the discount using this mutation:
mutation applyDiscount($cartId: ID!, $discountCode: String) {
discount: applyDiscount(cartId: $cartId, discountCode: $discountCode) {
discountCode
totalDiscount
discountId
value
target
trigger
type
warnings
}
}
Example variables:
{
"discountCode": "CODE",
"cartId": "65b54e63-71ab-439b-8f2c-5726a34daa97"
}
Successful response
{
"data": {
"discount": {
"discountCode": "CODE",
"discountId": "820143ab-40cc-4516-88af-963ea24d14c7",
"target": "all_products",
"totalDiscount": "7.00",
"trigger": "manual",
"type": "percentage",
"value": "100"
}
}
}
Error Response
{
"data": {
"discount": null
},
"errors": [
{
"locations": [
{
"column": 0,
"line": 2
}
],
"message": "Order must be a minimum of £5.00",
"path": [
"discount"
]
}
]
}
Response with warnings
{
"data": {
"discount": {
"warnings": [
"DISCOUNT_REQUIRES_CUSTOMER_LOGIN"
]
}
}
}
Types of warnings:
Warning | Meaning |
---|---|
DISCOUNT_REQUIRES_CUSTOMER_LOGIN | The discount requires a customer to be logged in before it can be validated. Once logged in they will be rejected if the customer does not meet the discount's criteria. |
Removing a discount
use this to remove a discount from a cart:
mutation removeDiscount($cartId: ID!) {
cart: removeDiscount(cartId: $cartId) {
id
}
}