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

Feature: Custom Facet Config #25

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
12 changes: 10 additions & 2 deletions class-facetwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,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),
Expand All @@ -210,7 +210,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);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions wp-graphql-facetwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);