7

I'm using Woo GraphQL and I'm wondering how I should go about getting the color of product attributes. See below for a screenshot of the product attribute in WP Admin:

enter image description here

I've tried querying for the top-level pa- attribute and I've tried querying in terms/termNode with no luck.

Am I missing something? How do I get this data?


Update: So TIL, that colors in products attributes were actually provided via "Variation Swatches for WooCommerce". Variation Swatches takes this info and saves it in term meta.

So now my question is a bit different: How do I pull term meta?

Rico Kahler
  • 17,616
  • 11
  • 59
  • 85
  • See docs, its is as simple as querying any normal taxonomy, make sure to pass the correct terms & taxonomy ids in your query : [Querying Custom Taxonomies](https://docs.wpgraphql.com/getting-started/custom-taxonomies/#querying-custom-taxonomies) – Maulik Parmar Mar 06 '20 at 09:47

2 Answers2

1

The best way that I have found is to not use (or with addition to ) WooSwatches plugin us should use advanced custom fields and WPGraphQL for Advanced Custom Fields in the following way

  • add a new field groupe with name Color Hex that has the location rule Taxonomy is equal to Color (or what ever attribute you want to use)

enter image description here

  • add a field with the name hex (for example) with type of color picker and make show in graphql

enter image description here

  • you should be able to query the data like this

paColors {
  nodes {
    id
    name
    description
    colorHex {
      hex
    }
  }
}

Mohamed SLimani
  • 351
  • 2
  • 9
  • This is also how i am doing it, but there should be a better way right? Did you find a better way in the mean while? – Wiezalditzijn May 26 '22 at 14:52
  • in fact this also gives me the option to pull the hex code and use it in the front end, so i prefer to use this way but there should be a better way if you only need the meta – Mohamed SLimani Jun 10 '22 at 01:56
0

You can get term meta using the get_term_meta() function. See more here.

I believe the meta key you want for this swatch_id.

Geoff Taylor
  • 496
  • 3
  • 17