Magento 2 How To Get Cross Sell Product GraphQL Query

In Magento 2, we can get all the cross-sell product items using cross sell product GraphQL query. To fetch the cross-sell products, we will need to apply the product SKU as a filter name to product GraphQL query. Before going with the cross sell product GraphQL query, you can check the full details of Product Query GraphQL

Cross Sell Product GraphQL Query Request Payload:

{
  products(filter: { sku: { eq: "test_1" } }) {
    items {
      id
      name
      crosssell_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: crosssell_products{} field is used to retrieve cross sell items of the product. You can set any field in request payload to retrieve the same field in response output from the product query.

Output:

{
  "data": {
    "products": {
      "items": [
        {
          "id": 1,
          "name": "Test 1",
          "crosssell_products": [
            {
              "id": 5,
              "sku": "test_cross_1",
              "stock_status": "IN_STOCK",
              "short_description": {
                "html": ""
              },
              "url_key": "test-cross-1",
              "name": "Test Cross 1",
              "special_price": null,
              "price_range": {
                "minimum_price": {
                  "final_price": {
                    "value": 15,
                    "currency": "USD"
                  }
                },
                "maximum_price": {
                  "final_price": {
                    "value": 15,
                    "currency": "USD"
                  }
                }
              }
            },
            {
              "id": 10,
              "sku": "test_cross_2",
              "stock_status": "IN_STOCK",
              "short_description": {
                "html": ""
              },
              "url_key": "test-cross-2",
              "name": "Test Cross 1",
              "special_price": null,
              "price_range": {
                "minimum_price": {
                  "final_price": {
                    "value": 30,
                    "currency": "USD"
                  }
                },
                "maximum_price": {
                  "final_price": {
                    "value": 30,
                    "currency": "USD"
                  }
                }
              }
            }
          ]
        }
      ]
    }
  }
}

We can add or remove the fields based on our custom requirement in the product query.

Hope this helps you guys!

Leave a Reply