Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to retrieve products by custom taxonomy #606

Closed
maximilliangeorge opened this issue Feb 9, 2022 · 6 comments
Closed

Unable to retrieve products by custom taxonomy #606

maximilliangeorge opened this issue Feb 9, 2022 · 6 comments
Milestone

Comments

@maximilliangeorge
Copy link

maximilliangeorge commented Feb 9, 2022

Describe the bug
I can't query posts by custom taxonomy on products, but it works on posts.

# This pattern works for ordinary posts
query WorksFine {
  posts {
    nodes {
      postModels {
        nodes {
          name
          posts {
            nodes {
              id
            }
          }
        }
      }
    }
  }
}

# But doesn't work for woocommerce products
query DoesNotWork {
  products {
    nodes {
      productModels {
        nodes {
          name
          products {
            nodes {
              id
            }
          }
        }
      }
    }
  }
}

To Reproduce
Steps to reproduce the behavior:

  1. Register custom taxonomies for both products and posts for comparison:
add_action('init', 'add_product_taxonomies', 0);

function add_product_taxonomies() {

  $product_labels = array(
    'name'                       => 'Product Models',
    'singular_name'              => 'Product Model',
    'menu_name'                  => 'Product Models',
    'all_items'                  => 'All Product Models',
    'parent_item'                => 'Parent Model',
    'parent_item_colon'          => 'Parent Model:',
    'new_item_name'              => 'New Product Model Name',
    'add_new_item'               => 'Add New Product Model',
    'edit_item'                  => 'Edit Product Model',
    'update_item'                => 'Update Product Model',
    'separate_items_with_commas' => 'Separate Models with commas',
    'search_items'               => 'Search Product Models',
    'add_or_remove_items'        => 'Add or remove Product Models',
    'choose_from_most_used'      => 'Choose from the most used Product Models',
  );

  register_taxonomy('productModels', 'product', array(
    'hierarchical' => false,
    'labels' => $product_labels,
    'show_in_graphql' => true,
    'graphql_single_name' => 'productModel',
    'graphql_plural_name' => 'productModels',
  ));

  $post_labels = array(
    'name'                       => 'Post Models',
    'singular_name'              => 'Post Model',
    'menu_name'                  => 'Post Models',
    'all_items'                  => 'All Post Models',
    'parent_item'                => 'Parent Model',
    'parent_item_colon'          => 'Parent Model:',
    'new_item_name'              => 'New Post Model Name',
    'add_new_item'               => 'Add New Post Model',
    'edit_item'                  => 'Edit Post Model',
    'update_item'                => 'Update Post Model',
    'separate_items_with_commas' => 'Separate Models with commas',
    'search_items'               => 'Search Post Models',
    'add_or_remove_items'        => 'Add or remove Post Models',
    'choose_from_most_used'      => 'Choose from the most used Post Models',
  );

  register_taxonomy('postModels', 'post', array(
    'hierarchical' => false,
    'labels' => $post_labels,
    'show_in_graphql' => true,
    'graphql_single_name' => 'postModel',
    'graphql_plural_name' => 'postModels',
  ));

}
  1. Create one or more posts using the Post Models taxonomy.
  2. Create one or more products using the Product Models Taxonomy
  3. Try querying the products and posts respectively. The product taxonomy does not contain any products.
# query

query WorksFineOnPosts {
  posts {
    nodes {
      postModels {
        nodes {
          name
          posts {
            nodes {
              id
            }
          }
        }
      }
    }
  }
}

# response

{
  "data": {
    "posts": {
      "nodes": [
        {
          "postModels": {
            "nodes": [
              {
                "name": "My Cool Model",
                "posts": {
                  "nodes": [
                    {
                      "id": "cG9zdDox"
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "extensions": {
    "debug": []
  }
}
# query

query DoesNotWorkOnProducts {
  products {
    nodes {
      productModels {
        nodes {
          name
          products {
            nodes {
              id
            }
          }
        }
      }
    }
  }
}

# response

{
  "errors": [
    {
      "message": "Cannot query field \"products\" on type \"ProductModel\".",
      "extensions": {
        "category": "graphql"
      },
      "locations": [
        {
          "line": 7,
          "column": 11
        }
      ]
    }
  ],
  "extensions": {
    "debug": []
  }
}

Expected behavior
Products should be exposed under a custom taxonomy for products, just like in posts.

Screenshots
N/A

Desktop (please complete the following information):

  • macOS
  • Safari/Chrome
  • Version 0.10.6

Smartphone (please complete the following information):

  • N/A

Plugin Versions

  • WooGraphQL Version: 0.10.6
  • WPGraphQL Version: 1.6.7
  • WordPress Version: 5.8.3
  • WooCommerce Version: 5.9.0

Additional context
N/A

@carstenjaksch
Copy link

Can confirm that. Taxonomy registered on post types post and page are working in GraphQL. But not product.

Only that works:

query NewQuery {
  products {
    nodes {
      name
      taxonomy {
        nodes {
          name
        }
      }
    }
  }
}

But not where.

@ryntab
Copy link

ryntab commented Jul 13, 2022

Have you tried using the taxonomy filter like this? In this example I am querying products by an array of inclusive and exclusive taxonomy objects.
GraphQL Query

query filterPacks(
  $Genres: [String]
  $GenresExclude: [String]
  $Formats: [String]
  $FormatsExclude: [String]
  $onSale: Boolean
) {
  products(
    where: {
      onSale: $onSale
      taxonomyFilter: {
        filters: [
          { taxonomy: FORMAT, terms: $Formats, operator: AND }
          { taxonomy: FORMAT, terms: $FormatsExclude, operator: NOT_IN }
          { taxonomy: GENRE, terms: $Genres, operator: AND }
          { taxonomy: GENRE, terms: $GenresExclude, operator: NOT_IN }
        ]
      }
      search: $search,
      status: "Publish"
    }
    first: $perPage
    before: $before
    after: $after
  )
}

Here is how the custom taxonomies were registered for WPGraphQL & WooGraphQL
functions.php

add_action('init', function () {
    register_taxonomy('product_genre', 'product', [
        'labels'  => [
            'menu_name' => __('Genres', 'your-textdomain'), //@see https://developer.wordpress.org/themes/functionality/internationalization/
        ],
        'show_in_graphql' => true,
        'graphql_single_name' => 'genre',
        'graphql_plural_name' => 'genres',
    ]);
    register_taxonomy('product_format', 'product', [
        'labels'  => [
            'menu_name' => __('Formats', 'your-textdomain'), //@see https://developer.wordpress.org/themes/functionality/internationalization/
        ],
        'show_in_graphql' => true,
        'graphql_single_name' => 'format',
        'graphql_plural_name' => 'formats',
    ]);
});

@carstenjaksch
Copy link

Yeah, I tried that. But GraphQL simply doesn't know about my taxonomy.

Register

add_action('init', function() {
	register_taxonomy('shop', array('post', 'page', 'product'), array(
		'labels' => array(
			'name' => 'Shops',
			'singular_name' => 'Shop',
			'search_items' => 'Shops suchen',
			'edit_item' => 'Shop bearbeiten',
			'update_item' => 'Shop aktualisieren',
			'menu_name' => 'Shops',
			'add_new_item' => 'Neuen Shop erstellen',
			'separate_items_with_commas' => 'Shops durch Komma trennen',
		),
		'show_admin_column' => true,
		'show_in_graphql' => true,
		'graphql_single_name' => 'shop',
		'graphql_plural_name' => 'shops',
	));
});

Query

query NewQuery {
  products(
    where: {taxonomyFilter: {filters: {taxonomy: SHOP, operator: AND, terms: "shop.domain.tld"}}}
  ) {
    edges {
      node {
        id
      }
    }
  }
}

Error

{
  "errors": [
    {
      "message": "Field \"products\" argument \"where\" requires type ProductTaxonomyEnum, found SHOP.",
      "extensions": {
        "category": "graphql"
      },
      "locations": [
        {
          "line": 3,
          "column": 50
        }
      ]
    }
  ],
  "extensions": {
    "debug": []
  }
}

@carstenjaksch
Copy link

Never mind. I use the plugin Code Snippets for this experiment and snippet scope needs to be everywhere, not just admin. taxonomyFilter is working fine, now.

That is an okay-ish workaround for me. Don't know about your use case, @maximilliangeorge.

@kidunot89
Copy link
Member

@maximilliangeorge Have you tried removing the 0 priority.

@kidunot89 kidunot89 added this to the v0.12.2 milestone Feb 7, 2023
@kidunot89
Copy link
Member

@maximilliangeorge Closing this due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants