Skip to main content

Managing Line Items

Adding a line to checkout

To add an item to the cart, use checkoutLinesAdd. The total price will be updated automatically:

mutation {
checkoutLinesAdd(
id: "Q2hlY2tvdXQ6ZTEzZDFjOTItOWJkNi00ODViLTgyMDctZTNhM2I5NjVkZTQw"
lines: [{ quantity: 1, variantId: "UHJvZHVjdFZhcmlhbnQ6Mjc0" }]
) {
checkout {
lines {
id
variant {
name
}
quantity
}
totalPrice {
gross {
currency
amount
}
}
}
}
}
Expand ▼

The response contains the updated checkout object:

{
"data": {
"checkoutLinesAdd": {
"checkout": {
"lines": [
{
"id": "Q2hlY2tvdXRMaW5lOjI1Mw=="
"variant": {
"name": "XL"
},
"quantity": 1
}
],
"totalPrice": {
"gross": {
"currency": "USD",
"amount": 5
}
}
}
}
}
}
Expand ▼

Adding the same product variant to the cart multiple times

info

This feature was introduced in Saleor 3.6.

caution

This feature is in the Feature Preview stage, which means that it is available for experimentation and feedback. However, it is still undergoing development and is subject to modifications.

Saleor's behavior when adding the same product variant to checkout multiple times varies depending on whether the forceNewLine flag is or was previously used:

  • forceNewLine is used - Saleor will create a new line with the provided quantity.
  • forceNewLine is not used and the variant exists in one line - Saleor will increase the corresponding line's quantity.
  • forceNewLine is not used and the variant exists in multiple lines - Saleor will create a new line with provided quantity

The forceNewLine can be used in CheckoutLineInput in the following mutations:

mutation {
checkoutLinesAdd(
id: "Q2hlY2tvdXQ6ZTEzZDFjOTItOWJkNi00ODViLTgyMDctZTNhM2I5NjVkZTQw"
lines: [
{ quantity: 1, variantId: "UHJvZHVjdFZhcmlhbnQ6Mjc0", forceNewLine: true }
{ quantity: 2, variantId: "UHJvZHVjdFZhcmlhbnQ6Mjc0" }
]
) {
checkout {
lines {
id
variant {
name
}
quantity
}
totalPrice {
gross {
currency
amount
}
}
}
}
}
Expand ▼

The response contains the updated checkout object:

{
"data": {
"checkoutLinesAdd": {
"checkout": {
"lines": [
{
"id": "Q2hlY2tvdXRMaW5lOjI1Mw==",
"variant": {
"name": "XL"
},
"quantity": 1
},
{
"id": "Q2hlY2tvdXRMaW5lOjI1Mw==",
"variant": {
"name": "XL"
},
"quantity": 2
}
],
"totalPrice": {
"gross": {
"currency": "USD",
"amount": 15
}
}
}
}
}
}
Expand ▼

If the variant is already added to the checkout and exists only in one line, Saleor will increase the corresponding line's quantity. Running mutation twice will result in a checkout object with one line and the amount set to 2:

mutation {
checkoutLinesAdd(
id: "Q2hlY2tvdXQ6ZTEzZDFjOTItOWJkNi00ODViLTgyMDctZTNhM2I5NjVkZTQw"
lines: [{ quantity: 1, variantId: "UHJvZHVjdFZhcmlhbnQ6Mjc0" }]
) {
checkout {
lines {
id
variant {
name
}
quantity
}
totalPrice {
gross {
currency
amount
}
}
}
}
}
Expand ▼

Response after second run:

{
"data": {
"checkoutLinesAdd": {
"checkout": {
"lines": [
{
"id": "Q2hlY2tvdXRMaW5lOjI1Mw=="
"variant": {
"name": "XL"
},
"quantity": 2
}
],
"totalPrice": {
"gross": {
"currency": "USD",
"amount": 10
}
}
}
}
}
}
Expand ▼
caution

If the variant is already added to the checkout and exists in more than one line, Saleor will create a new line with the provided quantity.

Updating a line

To modify a line, use checkoutLinesUpdate with lineId and quantity parameters.

Changing the line quantity

mutation {
checkoutLinesUpdate(
id: "Q2hlY2tvdXQ6ZTEzZDFjOTItOWJkNi00ODViLTgyMDctZTNhM2I5NjVkZTQw"
lines: [{ quantity: 12, lineId: "Q2hlY2tvdXRMaW5lOjI1NA==" }]
) {
checkout {
lines {
id
variant {
id
}
quantity
}
}
errors {
field
message
}
}
}
Expand ▼
{
"data": {
"checkoutLinesUpdate": {
"checkout": {
"lines": [
{
"id": "Q2hlY2tvdXRMaW5lOjI1NA==",
"variant": {
"id": "UHJvZHVjdFZhcmlhbnQ6MTcy"
},
"quantity": 12
}
]
},
"errors": []
}
}
}

If the quantity is changed to 0, API will remove this line from checkout.

Removing line from checkout

Running the checkoutLineDelete mutation will remove the whole line, no matter the quantity:

mutation {
checkoutLineDelete(
id: "Q2hlY2tvdXQ6ZTEzZDFjOTItOWJkNi00ODViLTgyMDctZTNhM2I5NjVkZTQw"
lineId: "Q2hlY2tvdXRMaW5lOjI1Mw=="
) {
checkout {
lines {
id
variant {
id
}
quantity
}
}
errors {
field
message
}
}
}
Expand ▼
{
"data": {
"checkoutLineDelete": {
"checkout": {
"lines": []
},
"errors": []
}
}
}

Setting custom prices for checkout lines

The variant price of any item in checkout can be overridden. The provided price will be treated as the base price of the variant. If you apply a voucher or sale in checkout, it will be used on top of the overridden price.

note

This feature is only available for apps with HANDLE_CHECKOUTS permission.

The custom price can be set with the price field in the CheckoutLineInput in the following mutations:

  • checkoutCreate,
  • checkoutLinesAdd – when adding a variant that already exists in the checkout, the corresponding line gets overridden – the quantity is incremented and the price is updated.
  • checkoutLinesUpdate – overrides the existing line with the price provided in the mutation.

The following example shows how to set a custom price with the use of checkoutLinesAdd mutation:

mutation {
checkoutLinesAdd(
id: "Q2hlY2tvdXQ6ZTEzZDFjOTItOWJkNi00ODViLTgyMDctZTNhM2I5NjVkZTQw"
lines: [
{ quantity: 1, variantId: "UHJvZHVjdFZhcmlhbnQ6MzA2", price: 16.22 }
]
) {
checkout {
id
lines {
variant {
id
}
quantity
totalPrice {
gross {
amount
currency
}
net {
amount
currency
}
}
}
}
errors {
field
message
}
}
}
Expand ▼

As a response, we get an updated checkout with the new price. As the given variant was already in the checkout, the quantity was incremented, and the price was overridden.

{
"data": {
"checkoutLinesAdd": {
"checkout": {
"id": "Q2hlY2tvdXQ6ZTEzZDFjOTItOWJkNi00ODViLTgyMDctZTNhM2I5NjVkZTQw",
"lines": [
{
"variant": {
"id": "UHJvZHVjdFZhcmlhbnQ6MzA2"
},
"quantity": 2,
"totalPrice": {
"gross": {
"amount": 32.44,
"currency": "USD"
},
"net": {
"amount": 32.44,
"currency": "USD"
}
}
}
]
},
"errors": []
}
}
}
Expand ▼