@@ -225,6 +225,7 @@ function() {
225225 'fromFieldName ' => 'parent ' ,
226226 'description ' => __ ( 'The parent of the node. The parent object can be of various types ' , 'wp-graphql-woocommerce ' ),
227227 'oneToOne ' => true ,
228+ 'queryClass ' => '\WC_Product_Query ' ,
228229 'resolve ' => function ( $ source , $ args , AppContext $ context , ResolveInfo $ info ) {
229230 if ( empty ( $ source ->parent_id ) ) {
230231 return null ;
@@ -243,27 +244,6 @@ function() {
243244 ]
244245 );
245246
246- // Taxonomy To Product resolver.
247- $ resolve_product_from_taxonomy = function ( $ source , array $ args , AppContext $ context , ResolveInfo $ info ) {
248- add_filter ( 'graphql_post_object_connection_args ' , [ __CLASS__ , 'bypass_get_args_sanitization ' ], 10 , 3 );
249- $ resolver = new PostObjectConnectionResolver ( $ source , $ args , $ context , $ info , 'product ' );
250- remove_filter ( 'graphql_post_object_connection_args ' , [ __CLASS__ , 'bypass_get_args_sanitization ' ], 10 , 3 );
251-
252- $ tax_query = [
253- [
254- // WPCS: slow query ok.
255- 'taxonomy ' => $ source ->taxonomyName , // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
256- 'field ' => 'term_id ' ,
257- 'terms ' => $ source ->term_id ,
258- ],
259- ];
260- $ resolver ->set_query_arg ( 'tax_query ' , $ tax_query );
261-
262- $ resolver = self ::set_ordering_query_args ( $ resolver , $ args );
263-
264- return $ resolver ->get_connection ();
265- };
266-
267247 // From WooCommerce product attributes.
268248 $ attributes = WP_GraphQL_WooCommerce::get_product_attribute_taxonomies ();
269249 foreach ( $ attributes as $ attribute ) {
@@ -275,7 +255,9 @@ function() {
275255 'fromFieldName ' => 'variations ' ,
276256 'resolve ' => function ( $ source , array $ args , AppContext $ context , ResolveInfo $ info ) {
277257 global $ wpdb ;
258+ add_filter ( 'graphql_post_object_connection_args ' , [ __CLASS__ , 'bypass_get_args_sanitization ' ], 10 , 3 );
278259 $ resolver = new PostObjectConnectionResolver ( $ source , $ args , $ context , $ info , 'product_variation ' );
260+ remove_filter ( 'graphql_post_object_connection_args ' , [ __CLASS__ , 'bypass_get_args_sanitization ' ], 10 , 3 );
279261
280262 // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
281263 $ attribute_meta_key = 'attribute_ ' . strtolower ( preg_replace ( '/([A-Z])/ ' , '_$1 ' , $ source ->taxonomyName ) );
@@ -291,8 +273,9 @@ function() {
291273 )
292274 );
293275
276+ $ variation_ids = ! empty ( $ variation_ids ) ? $ variation_ids : [ '0 ' ];
277+
294278 $ resolver ->set_query_arg ( 'post__in ' , $ variation_ids );
295- $ resolver ->set_query_arg ( 'post_type ' , 'product_variation ' );
296279
297280 $ resolver = self ::set_ordering_query_args ( $ resolver , $ args );
298281
@@ -304,6 +287,64 @@ function() {
304287 }//end foreach
305288 }
306289
290+ /**
291+ * Returns the singular name of all registered taxonomies connected the products.
292+ *
293+ * @return array
294+ */
295+ private static function get_product_connected_taxonomies () {
296+ $ taxonomies = [];
297+ $ allowed_taxonomies = \WPGraphQL::get_allowed_taxonomies ( 'objects ' );
298+
299+ foreach ( $ allowed_taxonomies as $ tax_object ) {
300+ if ( ! in_array ( 'product ' , $ tax_object ->object_type , true ) ) {
301+ continue ;
302+ }
303+
304+ $ taxonomies [] = ucfirst ( $ tax_object ->graphql_single_name );
305+ }
306+
307+ return $ taxonomies ;
308+ }
309+
310+ /**
311+ * Ensures all connection the `Product` type have proper connection config upon registration.
312+ *
313+ * @param array $config Connection config.
314+ * @return array
315+ */
316+ public static function set_connection_config ( $ config ) {
317+ $ to_type = $ config ['toType ' ];
318+ $ from_type = $ config ['fromType ' ];
319+ if ( 'Product ' === $ to_type ) {
320+ $ config ['connectionArgs ' ] = self ::get_connection_args ();
321+ $ config ['queryClass ' ] = '\WC_Product_Query ' ;
322+ }
323+
324+ $ taxonomies = self ::get_product_connected_taxonomies ();
325+ if ( 'Product ' === $ to_type && in_array ( $ from_type , $ taxonomies , true ) ) {
326+ $ config ['resolve ' ] = function ( $ source , array $ args , AppContext $ context , ResolveInfo $ info ) {
327+ add_filter ( 'graphql_post_object_connection_args ' , [ __CLASS__ , 'bypass_get_args_sanitization ' ], 10 , 3 );
328+ $ resolver = new PostObjectConnectionResolver ( $ source , $ args , $ context , $ info , 'product ' );
329+ remove_filter ( 'graphql_post_object_connection_args ' , [ __CLASS__ , 'bypass_get_args_sanitization ' ], 10 , 3 );
330+ $ tax_query = [
331+ [
332+ 'taxonomy ' => $ source ->taxonomyName , // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
333+ 'terms ' => [ $ source ->term_id ],
334+ 'field ' => 'term_id ' ,
335+ 'include_children ' => false ,
336+ ],
337+ ];
338+ $ resolver ->set_query_arg ( 'tax_query ' , $ tax_query );
339+
340+ $ resolver = self ::set_ordering_query_args ( $ resolver , $ args );
341+
342+ return $ resolver ->get_connection ();
343+ };
344+ }
345+ return $ config ;
346+ }
347+
307348 /**
308349 * Given an array of $args, this returns the connection config, merging the provided args
309350 * with the defaults
0 commit comments