Magento 2 How to get related products in GraphQL

If you want to get related products then you are show in this article, also we all know the related products block are appears at the bottom of the Product View page.

First of all we need to perform a products query for related products. Please check below and follow query which returns related products information.

Request

saveCopyzoom_out_map
{
  products(filter: { sku: { eq: "WJ08" } }) {
    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
      related_products {
        id
        name
        sku
        url_key
        __typename
      }
    }
  }
}

Response

saveCopyzoom_out_map
{
  "data": {
    "products": {
      "total_count": 1,
      "items": [
        {
          "id": 1322,
          "name": "Adrienne Trek Jacket",
          "sku": "WJ08",
          "url_key": "adrienne-trek-jacket",
          "stock_status": "IN_STOCK",
          "new_from_date": null,
          "new_to_date": null,
          "special_price": null,
          "special_from_date": null,
          "special_to_date": null,
          "__typename": "ConfigurableProduct",
          "related_products": [
            {
              "id": 1050,
              "name": "Mona Pullover Hoodlie",
              "sku": "WH01",
              "url_key": "mona-pullover-hoodlie",
              "__typename": "ConfigurableProduct"
            },
            {
              "id": 1120,
              "name": "Daphne Full-Zip Hoodie",
              "sku": "WH06",
              "url_key": "daphne-full-zip-hoodie",
              "__typename": "ConfigurableProduct"
            },
            {
              "id": 1860,
              "name": "Diana Tights",
              "sku": "WP06",
              "url_key": "diana-tights",
              "__typename": "ConfigurableProduct"
            },
            {
              "id": 1909,
              "name": "Portia Capri",
              "sku": "WP13",
              "url_key": "portia-capri",
              "__typename": "ConfigurableProduct"
            }
          ]
        }
      ]
    }
  }
}

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