Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get term meta WPGraphQL/WooGraphQL?

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?

like image 332
Rico Kahler Avatar asked Mar 02 '20 21:03

Rico Kahler


People also ask

What is wpgraphql and how does it work?

WPGraphQL allows you to separate your CMS from your presentation layer. Content creators can use the CMS they know, while developers can use the frameworks and tools they love. Query what you need.

How to expose WooCommerce data to wpgraphql?

This FREE plugin exposes WooCommerce data to WPGraphQL allowing you to interact with your store’s data via GraphQL Queries and mutations. Download the latest release from here or Clone / download the zip of this repository into your WordPress plugin directory, run composer install in plugin directory & activate the WP GraphQL WooCommerce plugin.

How is woographql different from other CPTs?

Unlike the most common CPTs which use a WP Post object as their data source and a WPGraphQL\Model\Post object as their model, WooGraphQL uses object managers as the data source for the CPTs and each individual has it's own model with it's own set of permissions and restrictions.

What is GraphQL and why should you care?

GraphQL queries allow access to multiple root resources, and also smoothly follow references between connected resources. While typical a REST API would require round-trip requests to many endpoints, GraphQL APIs can get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections.


1 Answers

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
    }
  }
}

like image 161
Mohamed SLimani Avatar answered Oct 19 '22 05:10

Mohamed SLimani