How can I query the colors that are already selected in the product in graphql?

How can I query the colors that are already selected in the product in graphql?

ViniciusGG
Shopify Partner
1 0 0

How can I check the colors that are already selected in the product?

For my query I am using getProduct followed by
productByHandle

My query:

ViniciusGG_1-1743717331406.png
Reuslt:
ViniciusGG_0-1743717303492.png

Color Admin:

ViniciusGG_2-1743717406728.pngViniciusGG_3-1743717426715.png

 

 

Reply 1 (1)

mageplaza-cs
Shopify Partner
474 40 77

Hi @ViniciusGG 

I am from Mageplaza - Shopify solution expert.

 

With your current query, you aren't fetching the selected colors for each variant yet because:

  • You're only fetching variants { node { id, title, price, availableForSale } }.
  • You haven't included the selectedOptions field inside each variant.

You need to update your query by adding selectedOptions inside node, like this:

variants(first: 5) {
  edges {
    node {
      id
      title
      price {
        amount
        currencyCode
      }
      availableForSale
      selectedOptions {
        name
        value
      }
    }
  }
}

 

Specifically, you need to:

  1. Add selectedOptions { name value } inside each variant.
  2. After that, filter for the options where name === "Color".

Example of the response after adding selectedOptions:

{
  "data": {
    "productByHandle": {
      "variants": {
        "edges": [
          {
            "node": {
              "id": "...",
              "title": "Red Shirt",
              "selectedOptions": [
                { "name": "Color", "value": "Red" },
                { "name": "Size", "value": "M" }
              ]
            }
          },
          {
            "node": {
              "id": "...",
              "title": "Blue Shirt",
              "selectedOptions": [
                { "name": "Color", "value": "Blue" },
                { "name": "Size", "value": "L" }
              ]
            }
          }
        ]
      }
    }
  }
}

=> This way, you can easily tell that the product has the colors Red and Blue.

 

Please let me know if it works as expected!

Best regards!

Mageplaza | Top-Rated Shopify Agency | Trusted by 230,000+ worldwide merchants


If our suggestion works for you, please give it a Like or mark it as a Solution!


Should you have any questions or concerns, feel free to contact us via consultant@mageplaza.com