Magento 2 How to get downloadable products in GraphQL

A Downloadable product type allows you to sell digital products, like eBooks, software applications, music, updates, games, etc. In this tutorial we will show you how to get a downloadable product, also you can show related attributes that are specific to downloadable products can be used when performing a product query.

The DownloadableProduct object contains the following attributes:

  • downloadable_product_links
  • downloadable_product_samples
  • links_purchased_separately
  • links_title

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

saveCopyzoom_out_map
... on DownloadableProduct {
  items {
   attributes
  }
}

The following products query returns DownloadableProduct information.

Request

saveCopyzoom_out_map
{
  products(filter: { sku: { eq: "240-LV09" } }) {
    total_count
    sort_fields {
      default
      options {
        label
        value
      }
    }
    aggregations {
      count
      attribute_code
      label
      options {
        count
        label
        value
      }
    }
    page_info {
      current_page
      total_pages
      page_size
    }
    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
      }
      sale
      new
      gender
      attribute_set_id
      meta_title
      meta_keyword
      meta_description
      manufacturer
      size
      color
      country_of_manufacture
      gift_message_available
      image {
        url
        label
      }
      small_image {
        url
        label
      }
      thumbnail {
        url
        label
      }
      swatch_image
      media_gallery {
        url
        label
      }
      categories {
        id
        name
        is_anchor
        url_key
        url_path
        level
        display_mode
        default_sort_by
        description
        breadcrumbs {
          category_id
          category_name
          category_level
          category_url_key
          category_url_path
        }
      }
      price_range {
        minimum_price {
          regular_price {
            value
            currency
          }
          final_price {
            value
            currency
          }
          discount {
            amount_off
            percent_off
          }
        }
        maximum_price {
          regular_price {
            value
            currency
          }
          final_price {
            value
            currency
          }
          discount {
            amount_off
            percent_off
          }
        }
      }
      price_tiers {
        quantity
        final_price {
          value
          currency
        }
      }
      ... on DownloadableProduct {
        links_title
        links_purchased_separately
        downloadable_product_links {
          sample_url
          sort_order
          title
          price
        }
        downloadable_product_samples {
          title
          sort_order
          sample_url
        }
      }
    }
  }
}

Response

saveCopyzoom_out_map
{
  "data": {
    "products": {
      "total_count": 1,
      "sort_fields": {
        "default": "position",
        "options": [
          {
            "label": "Position",
            "value": "position"
          },
          {
            "label": "Product Name",
            "value": "name"
          },
          {
            "label": "Price",
            "value": "price"
          }
        ]
      },
      "aggregations": [
        {
          "count": 1,
          "attribute_code": "price",
          "label": "Price",
          "options": [
            {
              "count": 1,
              "label": "9-*",
              "value": "9_*"
            }
          ]
        },
        {
          "count": 2,
          "attribute_code": "category_id",
          "label": "Category",
          "options": [
            {
              "count": 1,
              "label": "Training",
              "value": "9"
            },
            {
              "count": 1,
              "label": "Video Download",
              "value": "10"
            }
          ]
        },
        {
          "count": 2,
          "attribute_code": "activity",
          "label": "Activity",
          "options": [
            {
              "count": 1,
              "label": "Yoga",
              "value": "5435"
            },
            {
              "count": 1,
              "label": "Athletic",
              "value": "5443"
            }
          ]
        },
        {
          "count": 1,
          "attribute_code": "format",
          "label": "Format",
          "options": [
            {
              "count": 1,
              "label": "Download",
              "value": "5529"
            }
          ]
        }
      ],
      "page_info": {
        "current_page": 1,
        "total_pages": 1,
        "page_size": 20
      },
      "items": [
        {
          "id": 52,
          "name": "Luma Yoga For Life",
          "sku": "240-LV09",
          "url_key": "luma-yoga-for-life",
          "stock_status": "IN_STOCK",
          "new_from_date": null,
          "new_to_date": null,
          "special_price": null,
          "special_from_date": null,
          "special_to_date": null,
          "__typename": "DownloadableProduct",
          "short_description": {
            "html": "<p>\nLuma founder Erin Renny on yoga and longevity: \"I won't promise you'll live longer with yoga. No one can promise that. But your life will be healthier, less stressful, and more physically in tune when you focus on connecting your mind and body or a regular basis. Yoga is my favorite way to express this connection. I want to share the secrets of lifelong yoga with anyone willing to breathe and learn with me.\"\n</p>"
          },
          "description": {
            "html": "<ul><li>Increase strength + flexibility + metabolism</li><li>Burn calories + feel great</li><li>Gain energy + youthfulness + mental wellness</li> </ul><h3 style=\"margin-top: 30px;\">Download description</h3><p><strong style=\"display: block; margin: 20px 0 5px;\">Tone up mind and body</strong>Pro Yoga Instructor and Master Practitioner Marie Peale helps tone and sculpt your physique with her invigorating yet gentle approach.</p><p>You'll learn to use yoga to relax, control stress and increase your calorie-burning capacity, all while exploring traditional and new yoga poses that lengthen and strengthen your full muscular structure.  </p><ul><li>Easy download</li><li>Audio options: Music and instruction or instruction only</li><li>Heart rate techniques explained</li><li>Breathing techniques explained</li><li>Move through exercises at your own pace</li></ul><p>Two 25-minute workout episodes and one 40-minute workout episode with warm-up and cool down:</p><p><strong style=\"display: block; margin: 20px 0 5px;\">Episode 1</strong>Creative, fun session geared toward opening your lungs. Combines stretching and breathing with a focus on hips and shoulders. </p><p><strong style=\"display: block; margin: 20px 0 5px;\">Episode 2</strong>Ramp up the tempo and energy with calorie-burning flows. A stimulating workout introducing the body-sculpting power of yoga.</p><p><strong style=\"display: block; margin: 20px 0 5px;\">Episode 3</strong>Push your fitness reach and stamina with an intense series of standing and floor exercises and poses. End this extra-length session with a meditative cool down.</p><h3 style=\"margin-top: 30px;\">instructor bio</h3><p><strong style=\"display: block; margin: 20px 0 5px;\">About Marie</strong>Marie is a trained martial artist and dancer with a BS in Biological Engineering and over 10 years as a certified yoga teacher. Marie has studied yoga in England, India and, in 2009, at the Ashraqat Ashram Yoga Farm in the United States. She has been teaching full time since then. Her focus on strength and wellness combines a deep knowledge of human biology and motivation guided by unconditional love for her audience.</p>"
          },
          "sale": null,
          "new": null,
          "gender": null,
          "attribute_set_id": 14,
          "meta_title": null,
          "meta_keyword": null,
          "meta_description": "You'll learn to use yoga to relax, control stress and increase your calorie-burning capacity, all while exploring traditional and new yoga poses that lengthen and strengthen your full muscular structure.",
          "manufacturer": null,
          "size": null,
          "color": null,
          "country_of_manufacture": null,
          "gift_message_available": null,
          "image": {
            "url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/t/lt06.jpg",
            "label": "Luma Yoga For Life"
          },
          "small_image": {
            "url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/t/lt06.jpg",
            "label": "Luma Yoga For Life"
          },
          "thumbnail": {
            "url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/t/lt06.jpg",
            "label": "Luma Yoga For Life"
          },
          "swatch_image": null,
          "media_gallery": [
            {
              "url": "http://domain.com/pub/media/catalog/product/cache/1548177c389d2065a93f4e1eaab84907/l/t/lt06.jpg",
              "label": "Image"
            }
          ],
          "categories": [
            {
              "id": 9,
              "name": "Training",
              "is_anchor": 0,
              "url_key": "training",
              "url_path": "training",
              "level": 2,
              "display_mode": "PAGE",
              "default_sort_by": null,
              "description": null,
              "breadcrumbs": null
            },
            {
              "id": 10,
              "name": "Video Download",
              "is_anchor": 1,
              "url_key": "training-video",
              "url_path": "training/training-video",
              "level": 3,
              "display_mode": null,
              "default_sort_by": null,
              "description": null,
              "breadcrumbs": [
                {
                  "category_id": 9,
                  "category_name": "Training",
                  "category_level": 2,
                  "category_url_key": "training",
                  "category_url_path": "training"
                }
              ]
            }
          ],
          "price_range": {
            "minimum_price": {
              "regular_price": {
                "value": 0,
                "currency": "USD"
              },
              "final_price": {
                "value": 0,
                "currency": "USD"
              },
              "discount": {
                "amount_off": 0,
                "percent_off": 0
              }
            },
            "maximum_price": {
              "regular_price": {
                "value": 0,
                "currency": "USD"
              },
              "final_price": {
                "value": 0,
                "currency": "USD"
              },
              "discount": {
                "amount_off": 0,
                "percent_off": 0
              }
            }
          },
          "price_tiers": [],
          "links_title": "Downloads",
          "links_purchased_separately": 1,
          "downloadable_product_links": [
            {
              "sample_url": "http://domain.com/downloadable/download/linkSample/link_id/6/",
              "sort_order": 1,
              "title": "Episode 1",
              "price": 9
            },
            {
              "sample_url": "http://domain.com/downloadable/download/linkSample/link_id/7/",
              "sort_order": 1,
              "title": "Episode 2",
              "price": 9
            },
            {
              "sample_url": "http://domain.com/downloadable/download/linkSample/link_id/8/",
              "sort_order": 1,
              "title": "Episode 3",
              "price": 9
            }
          ],
          "downloadable_product_samples": [
            {
              "title": "Trailer #1",
              "sort_order": 1,
              "sample_url": "http://domain.com/downloadable/download/sample/sample_id/16/"
            },
            {
              "title": "Trailer #2",
              "sort_order": 1,
              "sample_url": "http://domain.com/downloadable/download/sample/sample_id/17/"
            },
            {
              "title": "Trailer #3",
              "sort_order": 1,
              "sample_url": "http://domain.com/downloadable/download/sample/sample_id/18/"
            }
          ]
        }
      ]
    }
  }
}

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.




Recent Articles
Tags
Newsletter
Copyright © 2023 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off