Sales (legacy)
Sales are automatically applied when a product is added to the checkout.
For instance, if a product is priced at $9 and has a 10% discount, it will be available for $8.1.
The discount will be visible on the line prices. To calculate the total applied discount,
subtract the line.undiscountedTotalPrice
from the line.totalPrice
.
To calculate the discount on one unit, subtract the line.undiscountedUnitPrice
from the line.unitPrice
.
The sale discount is not visible on checkout.discount.amount
.
In the example below, we add an on-sale product to the checkout.
The response of the checkoutLinesAdd
mutation will include the discounted product price.
mutation {
checkoutLinesAdd(
checkoutId: "Q2hlY2tvdXQ6NzMwMzkwMmItZGRhOC00MzU3LTk1YTAtNjRiODNiNTllMmUy"
lines: { quantity: 1, variantId: "UHJvZHVjdFZhcmlhbnQ6Mzcx" }
) {
errors {
message
}
checkout {
discount {
amount
}
token
lines {
quantity
variant {
name
}
totalPrice {
gross {
amount
}
net {
amount
}
}
undiscountedTotalPrice {
amount
}
unitPrice {
gross {
amount
}
net {
amount
}
}
undiscountedUnitPrice {
amount
}
}
}
}
}
Please note the line prices in the following response.
The line.totalPrice
is 8.1, and the line.undiscountedTotalPrice
is 9.0.
{
"data": {
"checkoutLinesAdd": {
"errors": [],
"checkout": {
"discount": {
"amount": 0.0
},
"token": "7303902b-dda8-4357-95a0-64b83b59e2e2",
"lines": [
{
"quantity": 1,
"variant": {
"name": "UHJvZHVjdFZhcmlhbnQ6Mzcx"
},
"totalPrice": {
"gross": {
"amount": 8.1
},
"net": {
"amount": 8.1
}
},
"undiscountedTotalPrice": {
"amount": 9.0
},
"unitPrice": {
"gross": {
"amount": 8.1
},
"net": {
"amount": 8.1
}
},
"undiscountedUnitPrice": {
"amount": 9.0
}
}
]
}
}
}
}
Sale and Voucher together
Sales and vouchers can be combined. In this case, the voucher discount is applied to the price after the sale. Let's consider an example: the checkout has two items, and the second item is on sale. A fixed discount of $5 is being applied to the entire order.
Here is the checkout data before applying the voucher code (only the sale is included in the price).
{
"data": {
"checkout": {
"id": "Q2hlY2tvdXQ6YzhkYmIxMWEtYzQyMi00ODdiLTg3ZTUtMGQ0NzhiNTU2N2Fj",
"token": "c8dbb11a-c422-487b-87e5-0d478b5567ac",
"channel": {
"slug": "default-channel"
},
"voucherCode": null,
"discount": {
"amount": 0.0
},
"discountName": null,
"totalPrice": {
"gross": {
"amount": 51.5
},
"net": {
"amount": 51.5
}
},
"subtotalPrice": {
"gross": {
"amount": 51.5
},
"net": {
"amount": 51.5
},
"__typename": "TaxedMoney"
},
"lines": [
{
"quantity": 1,
"undiscountedTotalPrice": {
"amount": 20.0
},
"totalPrice": {
"gross": {
"amount": 20.0
},
"net": {
"amount": 20.0
}
},
"unitPrice": {
"net": {
"amount": 20.0
}
},
"undiscountedUnitPrice": {
"amount": 20.0
}
},
{
"quantity": 1,
"undiscountedTotalPrice": {
"amount": 35.0
},
"totalPrice": {
"gross": {
"amount": 31.5
},
"net": {
"amount": 31.5
}
},
"unitPrice": {
"gross": {
"amount": 31.5
},
"net": {
"amount": 31.5
}
},
"undiscountedUnitPrice": {
"amount": 35.0
}
}
]
}
}
}
As we can see the price of the second line is reduced by $3.5. The checkout.discount
is $0.0.
Below is the checkout data after applying the entire order voucher.
{
"data": {
"checkoutAddPromoCode": {
"errors": [],
"checkout": {
"id": "Q2hlY2tvdXQ6YzhkYmIxMWEtYzQyMi00ODdiLTg3ZTUtMGQ0NzhiNTU2N2Fj",
"token": "c8dbb11a-c422-487b-87e5-0d478b5567ac",
"voucherCode": "DISCOUNT",
"discountName": "Big order discount",
"discount": {
"amount": 5.0
},
"subtotalPrice": {
"tax": {
"amount": 0.0
},
"gross": {
"amount": 46.5
},
"net": {
"amount": 46.5
}
},
"lines": [
{
"quantity": 1,
"totalPrice": {
"net": {
"amount": 18.06
},
"gross": {
"amount": 18.06
}
},
"undiscountedTotalPrice": {
"amount": 20.0
},
"unitPrice": {
"net": {
"amount": 18.06
},
"gross": {
"amount": 18.06
}
},
"undiscountedUnitPrice": {
"amount": 20.0
}
},
{
"quantity": 1,
"totalPrice": {
"net": {
"amount": 28.44
},
"gross": {
"amount": 28.44
}
},
"undiscountedTotalPrice": {
"amount": 35.0
},
"unitPrice": {
"net": {
"amount": 28.44
},
"gross": {
"amount": 28.44
}
},
"undiscountedUnitPrice": {
"amount": 35.0
}
}
]
}
}
}
As we can see, the total discount is $5.0. In the first line,
the entire difference between totalPrice
and undiscountedTotalPrice
comes from
the voucher discount ($1.94). However, in the second line, the difference
between those values ($6.56) is the total discount applied to this line,
which includes both sales and voucher discounts.
To calculate the value of the applied sale, we can sum up the line discounts and subtract
checkout.discount
. In this example, the calculation would be: ($1.94 + $6.56) - $5.00 = $3.5.
Therefore, we end up with the same value as before applying the voucher.
Completing checkout with a sale discount
In the case of a product on sale, the applied discount is visible only on the order and lines
prices; the order.discounts
field is empty. Let's see the following example,
for completing the checkout with two items of the product on sale.
{
"data": {
"checkoutComplete": {
"order": {
"id": "T3JkZXI6ZmQwNDFiMGMtZjFmYy00MjNjLTllMmUtOGJlOTY1ZDQwOGYy",
"status": "UNFULFILLED",
"totalCaptured": {
"amount": 133.51
},
"subtotal": {
"net": {
"amount": 52.83
},
"gross": {
"amount": 56.0
}
},
"total": {
"currency": "USD",
"net": {
"amount": 125.95
},
"gross": {
"amount": 133.51
}
},
"undiscountedTotal": {
"currency": "USD",
"net": {
"amount": 143.12
},
"gross": {
"amount": 147.51
}
},
"discounts": [],
"lines": [
{
"quantity": 2,
"totalPrice": {
"gross": {
"amount": 56.0
},
"net": {
"amount": 52.83
}
},
"unitPrice": {
"gross": {
"amount": 28.0
},
"net": {
"amount": 26.42
}
},
"undiscountedUnitPrice": {
"gross": {
"amount": 35.0
},
"net": {
"amount": 35.0
}
},
"unitDiscount": {
"amount": 7.0
}
}
]
},
"errors": []
}
}
}
As you can see, the order.discounts
field is empty, but there is a difference between
order.total
and order.undiscountedTotal
. The applied discount can be checked
on the unit level in the order.lines.unitDiscount
field, as well as by comparing
the order.lines.undiscountedUnitPrice
and order.lines.unitPrice
. The sum of line unit
discounts multiplied by the quantity of the line gives the total discount, which is
equal to the difference between order.total
and order.undiscountedTotal
.