In Magento 2, we can make use of Upsell product GraphQL query to retrieve all the upsell product items for the provided main product.
If any product has some assigned upsell products so we can fetch all the upsell items of the main product using the following GraphQL query:
Upsell Products GraphQL Query Request Payload:
{
products(filter: { sku: { eq: "test_1" } }) {
items {
id
name
upsell_products {
id
sku
stock_status
short_description {
html
}
url_key
name
special_price
price_range {
minimum_price {
final_price {
value
currency
}
}
maximum_price {
final_price {
value
currency
}
}
}
}
}
}
}
Note: upsell_products{}
field used to retrieve all the upsell product items of the main product.
Output:
{
"data": {
"products": {
"items": [
{
"id": 1,
"name": "Test Product 1",
"upsell_products": [
{
"id": 1,
"sku": "test_upsell_1",
"stock_status": "IN_STOCK",
"short_description": {
"html": ""
},
"url_key": "test-upsell-1",
"name": "Test Upsell 1",
"special_price": null,
"price_range": {
"minimum_price": {
"final_price": {
"value": 10,
"currency": "USD"
}
},
"maximum_price": {
"final_price": {
"value": 10,
"currency": "USD"
}
}
}
},
{
"id": 2,
"sku": "test_2",
"stock_status": "IN_STOCK",
"short_description": {
"html": ""
},
"url_key": "test-upsell-2",
"name": "Test upsell 2",
"special_price": null,
"price_range": {
"minimum_price": {
"final_price": {
"value": 20,
"currency": "USD"
}
},
"maximum_price": {
"final_price": {
"value": 20,
"currency": "USD"
}
}
}
}
]
}
]
}
}
}