From f5b3402a9436c5ba06d507763e14008610f2e58e Mon Sep 17 00:00:00 2001 From: Matteo Demicheli Date: Sun, 19 Jun 2022 23:57:40 +0200 Subject: [PATCH] Feature: Custom Facet Config (#25) * Add filter for facet connection * Add docs for custom facet config --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ class-facetwp.php | 12 ++++++++++-- wp-graphql-facetwp.php | 4 ++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fd1998f..b9339a6 100644 --- a/README.md +++ b/README.md @@ -57,5 +57,43 @@ query GetPosts($query: FacetQueryArgs, $after: String, $search: String, $orderBy } ``` +## WooCommerce Support + +Support for WooCommerce Products can be added with following configuration: + +```php +add_action('graphql_register_types', function () { + register_graphql_facet_type('product'); +}); + +add_filter('facetwp_graphql_facet_connection_config', function (array $default_graphql_config, array $config) { + $type = $config['type']; + $singular = $config['singular']; + $field = $config['field']; + $plural = $config['plural']; + + return [ + 'fromType' => $field, + 'toType' => $singular, + 'fromFieldName' => lcfirst($plural), + 'connectionArgs' => Products::get_connection_args(), + 'resolveNode' => function ($node, $_args, $context) use ($type) { + return $context->get_loader($type)->load_deferred($node->ID); + }, + 'resolve' => function ($source, $args, $context, $info) use ($type) { + $resolver = new PostObjectConnectionResolver($source, $args, $context, $info, $type); + + if ($type === 'product') { + $resolver = Products::set_ordering_query_args( $resolver, $args ); + } + + return $resolver + ->set_query_arg('post__in', $source['results']) + ->get_connection(); + }, + ]; +}, 100, 2); +``` + ## Limitations Currently the plugin only has been tested using Checkbox and Radio facet types. Support for additional types is in development. diff --git a/class-facetwp.php b/class-facetwp.php index 6dbc2b0..7ae4a73 100644 --- a/class-facetwp.php +++ b/class-facetwp.php @@ -242,7 +242,7 @@ private function register_facet_connection($config) $field = $config['field']; $plural = $config['plural']; - register_graphql_connection([ + $default_facet_connection_config = [ 'fromType' => $field, 'toType' => $singular, 'fromFieldName' => lcfirst($plural), @@ -261,7 +261,15 @@ private function register_facet_connection($config) ->set_query_arg('post__in', $source['results']) ->get_connection(); }, - ]); + ]; + + $graphql_connection_config = apply_filters( + 'facetwp_graphql_facet_connection_config', + $default_facet_connection_config, + $config + ); + + register_graphql_connection($graphql_connection_config); } /** diff --git a/wp-graphql-facetwp.php b/wp-graphql-facetwp.php index 7431f21..c872085 100644 --- a/wp-graphql-facetwp.php +++ b/wp-graphql-facetwp.php @@ -69,3 +69,7 @@ function () use ($versions) { return false; } }); + +add_filter('facetwp_graphql_facet_connection_config', function (array $default_graphql_config) { + return $default_graphql_config; +}, 10, 1);