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

Unable to retrieve products by custom taxonomy

Open maximilliangeorge opened this issue 3 years ago • 5 comments
trafficstars

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

maximilliangeorge avatar Feb 09 '22 19:02 maximilliangeorge

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.

carstenjaksch avatar Jul 13 '22 07:07 carstenjaksch

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',
    ]);
});

ryntab avatar Jul 13 '22 07:07 ryntab

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 avatar Jul 14 '22 09:07 carstenjaksch

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.

carstenjaksch avatar Jul 14 '22 09:07 carstenjaksch

@maximilliangeorge Have you tried removing the 0 priority.

kidunot89 avatar Aug 25 '22 14:08 kidunot89

@maximilliangeorge Closing this due to inactivity.

kidunot89 avatar Jun 21 '23 18:06 kidunot89