Magento 2 How to get grouped products in GraphQL
Grouped Product is a collection of Simple products that are already created in Magento 2. Here you can look at how to get grouped products, also used products query for specific attributes.
GroupedProduct object contains items and GroupedProductItem object contains qty, position and product.
Add the following inline fragment to the output section of your products query to return information specific to grouped products:
... on GroupedProduct {
items {
attributes
}
}
The following query returns GroupedProduct information.
Request
saveCopyzoom_out_map{
products(filter: { sku: { eq: "24-WG085_Group" } }) {
total_count
items {
id
name
sku
url_key
stock_status
new_from_date
new_to_date
special_price
special_from_date
special_to_date
__typename
short_description {
html
}
description {
html
}
image {
url
label
}
small_image {
url
label
}
thumbnail {
url
label
}
swatch_image
media_gallery {
url
label
}
... on GroupedProduct {
id
name
sku
items {
qty
position
product {
id
sku
name
stock_status
__typename
url_key
}
}
}
}
}
}
Response
saveCopyzoom_out_map{
"data": {
"products": {
"total_count": 1,
"items": [
{
"id": 45,
"name": "Set of Sprite Yoga Straps",
"sku": "24-WG085_Group",
"url_key": "set-of-sprite-yoga-straps",
"stock_status": "IN_STOCK",
"new_from_date": null,
"new_to_date": null,
"special_price": null,
"special_from_date": null,
"special_to_date": null,
"__typename": "GroupedProduct",
"short_description": {
"html": ""
},
"description": {
"html": "<p>Great set of Sprite Yoga Straps for every stretch and hold you need. There are three straps in this set: 6', 8' and 10'.</p>\n<ul>\n<li> 100% soft and durable cotton.\n<li> Plastic cinch buckle is easy to use.\n<li> Choice of three natural colors made from phthalate and heavy metal free dyes.\n</ul>"
},
"image": {
"url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/u/luma-yoga-strap-set.jpg",
"label": "Set of Sprite Yoga Straps"
},
"small_image": {
"url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/u/luma-yoga-strap-set.jpg",
"label": "Set of Sprite Yoga Straps"
},
"thumbnail": {
"url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/u/luma-yoga-strap-set.jpg",
"label": "Set of Sprite Yoga Straps"
},
"swatch_image": null,
"media_gallery": [
{
"url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/u/luma-yoga-strap-set.jpg",
"label": "Image"
}
],
"items": [
{
"qty": 0,
"position": 0,
"product": {
"id": 33,
"sku": "24-WG085",
"name": "Sprite Yoga Strap 6 foot",
"stock_status": "IN_STOCK",
"__typename": "SimpleProduct",
"url_key": "sprite-yoga-strap-6-foot"
}
},
{
"qty": 0,
"position": 1,
"product": {
"id": 34,
"sku": "24-WG086",
"name": "Sprite Yoga Strap 8 foot",
"stock_status": "IN_STOCK",
"__typename": "SimpleProduct",
"url_key": "sprite-yoga-strap-8-foot"
}
},
{
"qty": 0,
"position": 2,
"product": {
"id": 35,
"sku": "24-WG087",
"name": "Sprite Yoga Strap 10 foot",
"stock_status": "IN_STOCK",
"__typename": "SimpleProduct",
"url_key": "sprite-yoga-strap-10-foot"
}
}
]
}
]
}
}
}
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.