wp-graphql-woocommerce icon indicating copy to clipboard operation
wp-graphql-woocommerce copied to clipboard

No nodes on products.nodes[n].productCategories

Open justlevine opened this issue 3 years ago • 6 comments

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 gql

Additional context Wordpress: 5.6 wp-graphql: 1.0.0 wp-graphql-woocommerce: 0.6.1

justlevine avatar Nov 25 '20 08:11 justlevine

@justlevine Can you update to v0.7.0? You maybe experiencing this issue

kidunot89 avatar Nov 26 '20 19:11 kidunot89

@kidunot89 Sadly, the issue persists in v0.7.0...

justlevine avatar Dec 01 '20 00:12 justlevine

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.

justlevine avatar Dec 01 '20 02:12 justlevine

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 avatar Dec 01 '20 03:12 justlevine

@justlevine I believe the issue maybe with your product configurations. This is the result when I run you're query locally and in playground image image

Is there something operating as a middleman to set these categories like another Woo extension or WP plugin?

kidunot89 avatar Dec 02 '20 00:12 kidunot89

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?

justlevine avatar Dec 02 '20 01:12 justlevine