Magento 2 How to get cart information from login customer using GraphQL
When customer login and get cart information, use customerCart
query, we will explain you how to get cart information from logged in customer.
The customerCart
query must be run on behalf of a logged in customer.
If you run this query on behalf of a guest, an exception will be thrown.
The cart query requires a cart_id
value as input, but this query does not take any input parameters.
The following query lists item with configurable product in logged in customer's cart.
Request
saveCopyzoom_out_map{
customerCart{
id
email
items{
id
quantity
product{
id
sku
name
}
... on ConfigurableCartItem{
id
configurable_options{
id
value_id
value_label
option_label
}
}
}
}
}
Response
saveCopyzoom_out_map{
"data": {
"customerCart": {
"id": "aM0TxBycrhdQdwRn1HjXyeOWK8g30n3c",
"email": "devtest@example.com",
"items": [
{
"id": "23",
"quantity": 1,
"product": {
"id": 340,
"sku": "MJ08",
"name": "Lando Gym Jacket"
},
"configurable_options": [
{
"id": 144,
"value_id": 5595,
"value_label": "M",
"option_label": "Size"
},
{
"id": 93,
"value_id": 5479,
"value_label": "Gray",
"option_label": "Color"
}
]
},
{
"id": "25",
"quantity": 1,
"product": {
"id": 3,
"sku": "24-MB03",
"name": "Crown Summit Backpack"
}
}
]
}
}
}
That's it!!
If you are looking for more GraphQL solutions then check out our another article which is List of GraphQL Queries and Mutations for Magento 2.
I hope this article helps you to find what you were looking for.
Bookmark it for your future reference. Do comment below if you have any other questions on that.
P.S. Do share this article with your team.