Magento 2 How to get Bundle Products in GraphQL

We all know Bundle products are almost similar to Grouped products and in this article teach you how to get bundle products. Also we know that each item in a bundle can be based on one of the Simple Product and Virtual Product types.

  • The BundleProduct object contains dynamic_sku, dynamic_price, dynamic_weight, etc ...
  • The BundleItem object contains option_id, title, sku, type, etc ...
  • The BundleItemOption object contains id, label, quantity, can_change_quantity, etc ...

Add the following inline fragment to the output section of your products query to return information to bundle products:

Copy
    .. on BundleProduct {
    dynamic_sku
    dynamic_price
    dynamic_weight
    items {
    attributes
    }
    }

The following query returns BundleProduct information.

Request

Copy
{
  products(filter: { sku: { eq: "24-WG080" } }) {
    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 BundleProduct {
        dynamic_sku
        dynamic_price
        dynamic_weight
        price_view
        ship_bundle_items
        items {
          option_id
          title
          sku
          type
          required
          position
          options {
            id
            label
            quantity
            can_change_quantity
            price
            price_type
            is_default
            position
            product {
              id
              name
              sku
              stock_status
              __typename
            }
          }
        }
      }
    }
  }
}

Response

Copy
{
  "data": {
    "products": {
      "total_count": 1,
      "items": [
        {
          "id": 46,
          "name": "Sprite Yoga Companion Kit",
          "sku": "24-WG080",
          "url_key": "sprite-yoga-companion-kit",
          "stock_status": "IN_STOCK",
          "new_from_date": null,
          "new_to_date": null,
          "special_price": null,
          "special_from_date": null,
          "special_to_date": null,
          "__typename": "BundleProduct",
          "short_description": {
            "html": ""
          },
          "description": {
            "html": "<p>A well-rounded yoga workout takes more than a mat. The Sprite Yoga Companion Kit helps stock your studio with the basics you need for a full-range workout. The kit is composed of four best-selling Luma Sprite accessories in one easy bundle: statis ball, foam block, yoga strap, and foam roller. Choose sizes and colors and leave the rest to us. The kit includes:</p>\n<ul>\n<li> Sprite Statis Ball\n<li> Sprite Foam Yoga Brick\n<li> Sprite Yoga Strap\n<li> Sprite Foam Roller\n</ul>"
          },
          "image": {
            "url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/u/luma-yoga-kit-2.jpg",
            "label": "Sprite Yoga Companion Kit"
          },
          "small_image": {
            "url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/u/luma-yoga-kit-2.jpg",
            "label": "Sprite Yoga Companion Kit"
          },
          "thumbnail": {
            "url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/u/luma-yoga-kit-2.jpg",
            "label": "Sprite Yoga Companion Kit"
          },
          "swatch_image": null,
          "media_gallery": [
            {
              "url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/u/luma-yoga-kit-2.jpg",
              "label": "Image"
            }
          ],
          "dynamic_sku": true,
          "dynamic_price": true,
          "dynamic_weight": true,
          "price_view": "PRICE_RANGE",
          "ship_bundle_items": "TOGETHER",
          "items": [
            {
              "option_id": 1,
              "title": "Sprite Stasis Ball",
              "sku": "24-WG080",
              "type": "radio",
              "required": true,
              "position": 1,
              "options": [
                {
                  "id": 1,
                  "label": "Sprite Stasis Ball 55 cm",
                  "quantity": 1,
                  "can_change_quantity": true,
                  "price": 0,
                  "price_type": "FIXED",
                  "is_default": true,
                  "position": 1,
                  "product": {
                    "id": 26,
                    "name": "Sprite Stasis Ball 55 cm",
                    "sku": "24-WG081-blue",
                    "stock_status": "IN_STOCK",
                    "__typename": "SimpleProduct"
                  }
                },
                {
                  "id": 2,
                  "label": "Sprite Stasis Ball 65 cm",
                  "quantity": 1,
                  "can_change_quantity": true,
                  "price": 0,
                  "price_type": "FIXED",
                  "is_default": false,
                  "position": 2,
                  "product": {
                    "id": 29,
                    "name": "Sprite Stasis Ball 65 cm",
                    "sku": "24-WG082-blue",
                    "stock_status": "IN_STOCK",
                    "__typename": "SimpleProduct"
                  }
                },
                {
                  "id": 3,
                  "label": "Sprite Stasis Ball 75 cm",
                  "quantity": 1,
                  "can_change_quantity": true,
                  "price": 0,
                  "price_type": "FIXED",
                  "is_default": false,
                  "position": 3,
                  "product": {
                    "id": 32,
                    "name": "Sprite Stasis Ball 75 cm",
                    "sku": "24-WG083-blue",
                    "stock_status": "IN_STOCK",
                    "__typename": "SimpleProduct"
                  }
                }
              ]
            },
            {
              "option_id": 2,
              "title": "Sprite Foam Yoga Brick",
              "sku": "24-WG080",
              "type": "radio",
              "required": true,
              "position": 2,
              "options": [
                {
                  "id": 4,
                  "label": "Sprite Foam Yoga Brick",
                  "quantity": 1,
                  "can_change_quantity": true,
                  "price": 0,
                  "price_type": "FIXED",
                  "is_default": true,
                  "position": 1,
                  "product": {
                    "id": 21,
                    "name": "Sprite Foam Yoga Brick",
                    "sku": "24-WG084",
                    "stock_status": "IN_STOCK",
                    "__typename": "SimpleProduct"
                  }
                }
              ]
            },
            {
              "option_id": 3,
              "title": "Sprite Yoga Strap",
              "sku": "24-WG080",
              "type": "radio",
              "required": true,
              "position": 3,
              "options": [
                {
                  "id": 5,
                  "label": "Sprite Yoga Strap 6 foot",
                  "quantity": 1,
                  "can_change_quantity": true,
                  "price": 0,
                  "price_type": "FIXED",
                  "is_default": true,
                  "position": 1,
                  "product": {
                    "id": 33,
                    "name": "Sprite Yoga Strap 6 foot",
                    "sku": "24-WG085",
                    "stock_status": "IN_STOCK",
                    "__typename": "SimpleProduct"
                  }
                },
                {
                  "id": 6,
                  "label": "Sprite Yoga Strap 8 foot",
                  "quantity": 1,
                  "can_change_quantity": true,
                  "price": 0,
                  "price_type": "FIXED",
                  "is_default": false,
                  "position": 2,
                  "product": {
                    "id": 34,
                    "name": "Sprite Yoga Strap 8 foot",
                    "sku": "24-WG086",
                    "stock_status": "IN_STOCK",
                    "__typename": "SimpleProduct"
                  }
                },
                {
                  "id": 7,
                  "label": "Sprite Yoga Strap 10 foot",
                  "quantity": 1,
                  "can_change_quantity": true,
                  "price": 0,
                  "price_type": "FIXED",
                  "is_default": false,
                  "position": 3,
                  "product": {
                    "id": 35,
                    "name": "Sprite Yoga Strap 10 foot",
                    "sku": "24-WG087",
                    "stock_status": "IN_STOCK",
                    "__typename": "SimpleProduct"
                  }
                }
              ]
            },
            {
              "option_id": 4,
              "title": "Sprite Foam Roller",
              "sku": "24-WG080",
              "type": "radio",
              "required": true,
              "position": 4,
              "options": [
                {
                  "id": 8,
                  "label": "Sprite Foam Roller",
                  "quantity": 1,
                  "can_change_quantity": true,
                  "price": 0,
                  "price_type": "FIXED",
                  "is_default": true,
                  "position": 1,
                  "product": {
                    "id": 22,
                    "name": "Sprite Foam Roller",
                    "sku": "24-WG088",
                    "stock_status": "IN_STOCK",
                    "__typename": "SimpleProduct"
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  }
}

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.




Search
Recent Posts
    Tags
    Newsletter
    SocialFollow on Twitter
    Visit now!