wp-graphql-woocommerce
wp-graphql-woocommerce copied to clipboard
No nodes on products.nodes[n].productCategories
Describe the bug
Im trying to get productsCategories
on a products
query, but its coming back empty.
To Reproduce
in WPGraphiql
query getProducts { products(first: 50) { nodes { id databaseId slug description name productCategories { nodes { name uri id slug } } } } }
Expected behavior
there should be an array of productCategories. but instead its []
Screenshots
Additional context Wordpress: 5.6 wp-graphql: 1.0.0 wp-graphql-woocommerce: 0.6.1
@justlevine Can you update to v0.7.0? You maybe experiencing this issue
@kidunot89 Sadly, the issue persists in v0.7.0...
Throwing in random error_logs, class-wc-terms.php
:: register_connections()
gets the correct $term_ids
, and it seems to stay until TermObjectConnectionResolver->get_ids()
. TermObjectConnectionResolver->get_query
has terms
empty.
On further inspection, it seems that the issue is that the plugin is using the term_id
for the term_taxonomy_id
.
So for a (real world example)
WP_Term Object
(
[term_id] => 269
[name] => Rentals
[slug] => rentals
[term_group] => 0
[term_taxonomy_id] => 271
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 7
[filter] => raw
[term_icon] =>
)
graphql will return the productCategories
empty.
@kidunot89 The issue is in class-wc-terms.php::register_connections().
The solution is seemingly to change lines 54 and 56 so they're both using term_taxonomy_id
.
E.g.
$term_taxonomy_ids = \wc_get_object_terms( $source->ID, $tax_object->name, 'term_taxonomy_id' );
$resolver->set_query_arg( 'term_taxonomy_id', ! empty( $term_taxonomy_ids) ? $term_taxonomy_ids : array( '0' ) );
I'm not yet familiar enough with the codebase to feel comfortable submitting a PR without knowing the side effects of using term_id
vs term_taxonomy_id
, but so far with the change made locally (tried it both ways), everything is working as expected.
(edited: accidently wrote taxonomy_term_id
instead of term_taxonomy_id
)
@justlevine I believe the issue maybe with your product configurations.
This is the result when I run you're query locally and in playground
Is there something operating as a middleman to set these categories like another Woo extension or WP plugin?
My tests were run on a clean install with only WC, WPGraphQL, WooGraphQL and JWT, but I did use product data imported from a live site (which has bookings and subscriptions), which might be why the term_id
and the term_taxonomy_id
are different in this instance.
Regardless of how it happened, it does seem that there are valid use cases when the term_id and the term_taxonomy_id would be different (from my googling https://wordpress.stackexchange.com/questions/37721/whats-the-difference-between-term-id-and-term-taxonomy-id and https://salferrarello.com/term_taxonomy_id-vs-term_id/), so unless there's side effects I'm unaware of, this plugin should add the query_arg that it actually fetched, and not use the two args interchangeably, no?