Wp-graphql-woocommerce: [release 0.6.1] Querying a Custom Taxonomy on Product will list all terms, not just the ones associated to the product

Created on 21 Oct 2020  路  3Comments  路  Source: wp-graphql/wp-graphql-woocommerce

Describe the bug
I have a custom taxonomy on Product called: Collections.

For collections, I have the terms Spring/Summer, and Fall/Winter.

I have a single product and have assigned the term Spring/Summer to it.

However, when I query the product like so:

{
    products {
        collections {
            nodes {
                name
            }
        }
    }
}

I receive both Spring/Summer and Fall/Winter instead of just Spring/Summer. I believe this is a regression from v0.5.1 since the expected behaviour was to return only the linked terms of the taxonomy.

bug

Most helpful comment

@ardiewen I think I see the issue with my PR. I mistakenly assumed all requests coming through would be $attributes, not taxonomies.

@kidunot89 Using get_the_terms() works better for this issue and my issue. Want me to get another PR rolling?

$resolver = new TermObjectConnectionResolver( $source, $args, $context, $info, $tax_object->name );

$terms = wp_list_pluck( get_the_terms( $source->ID, $tax_object->name ), 'term_id' );

$resolver->set_query_arg( 'term_taxonomy_id', ! empty( $terms ) ? $terms : array( '0' ) );

return $resolver->get_connection();

All 3 comments

Swapped over to the develop branch of the plugin with #346 applied and the opposite is actually happening now.

When querying a custom taxonomy, like above, the result is an empty array of nodes even when there are terms associated with the product. E.g.

Query:

{
  products {
    nodes {
      collections {
        nodes {
          name
        }
      }
    }
  }
}

Result:

{
  "data": {
    "products": {
      "nodes": [
        {
          "collections": {
            "nodes": []
          }
        },
        {
          "collections": {
            "nodes": []
          }
        },
        {
          "collections": {
            "nodes": []
          }
        },
        {
          "collections": {
            "nodes": []
          }
        },
        {
          "collections": {
            "nodes": []
          }
        },
        {
          "collections": {
            "nodes": []
          }
        }
      ]
    }
  }
}

@ardiewen I think I see the issue with my PR. I mistakenly assumed all requests coming through would be $attributes, not taxonomies.

@kidunot89 Using get_the_terms() works better for this issue and my issue. Want me to get another PR rolling?

$resolver = new TermObjectConnectionResolver( $source, $args, $context, $info, $tax_object->name );

$terms = wp_list_pluck( get_the_terms( $source->ID, $tax_object->name ), 'term_id' );

$resolver->set_query_arg( 'term_taxonomy_id', ! empty( $terms ) ? $terms : array( '0' ) );

return $resolver->get_connection();

@ardiewen I've got the PR up, so hopefully it solves both of our issues once it's merged in. It's testing good for me (both attributes and product categories).

Was this page helpful?
0 / 5 - 0 ratings