Could you give me some guide or tutorial how to connect yours plugin to woocommerce and woocommerce rest api. I was searching information about it in your docs but did not find anything.
@oleksmir accidentally closed this. re-opened.
I'm not super familiar with the WooCommerce REST API, but you can add whatever Types you want to the WPGraphQL Schema and have them resolve to whatever underlying data you want.
Since WooCommerce (for now) uses Custom Post Types and Custom Taxonomies, you should be able to expose them to WPGraphQL by setting those post types to show_in_graphql and providing a graphql_single_name and graphql_plural_name (see: https://github.com/wp-graphql/wp-graphql/wiki/Extending-and-Customizing#expose-custom-post-type-or-custom-taxonomy-to-graphql-query)
Then, to add custom fields to the Types, you can filter in new fields to each of the Types.
So, in WooCommerce, there is a products Custom Post Type, so let's say you added that to WPGraphQL like so:
add_filter( 'register_post_type_args', function( $args, $post_type ) {
if ( 'product' === $post_type ) {
$args['show_in_graphql'] = true;
$args['graphql_single_name'] => 'product';
$args['graphql_plural_name'] => 'products';
}
return $args;
}, 10, 2 );
You would now have a Product type in the WPGraphQL Schema that you could add fields too, like so:
add_filter( 'graphql_Product_fields, function( $fields ) {
$fields['yourCustomField'] => [
'type' => \WPGraphQL\Types::string(),
'description' => __( 'Description of your field', 'your-textdomain' ),
'resolve' => function( $product ) {
// Get your data for this field
$value = get_post_meta( $product->ID, 'your_custom_field_meta_key', true );
return ! empty( $value ) ? $value : null;
}
];
return $fields;
} );
I'm not familiar with how WooCommerce registers it's meta fields, but there's likely something you could do to get the fields that are registered to WooCommerce Post Types and loop through that existing registration and have it automatically add to WPGraphQL.
You can see how @roborourke does something similar to dynamically add any meta fields that were added using register_meta here: https://github.com/roborourke/wp-graphql-meta and take that concept and apply it to whatever static field registration WooCommerce uses? I'd have to get more familiar with WooCommerce to provide more solid insight. . .I haven't used it in 4+ years so I'm not super familiar with its codebase anymore.
Hope this helps.
If you come up with anything good, please share! Would love to see a good WooCommerce + WPGraphQL extension or something to that tune.
Hm, probably I'm too far away from WP to understand how it should be implemented, Main idea - wp-graphql is not enough to achieve expected results? Only create some additional plugin. Yes?
wp-graphql is _enough_ to achieve expected results...
First step : Expose the Woocommerce product custom post type (use WordPress filter hook)
add_filter( 'register_post_type_args', function( $args, $post_type ) {
if ( 'product' === $post_type ) {
$args['show_in_graphql'] = true;
$args['graphql_single_name'] = 'product';
$args['graphql_plural_name'] = 'products';
}
return $args;
}, 10, 2 );
Second step : Resolve WooCommerce meta fields (example of _sku using a WordPress action hook). You can resolve as many meta fields as you want.
add_action( 'graphql_register_types', function() {
register_graphql_field( 'product', '_sku', [
'type' => 'String',
'description' => __( 'The sku of the product', 'wp-graphql' ),
'resolve' => function( $post ) {
$value = get_post_meta( $post->ID, '_sku', true );
return ! empty( $value ) ? $value : null;
}
] );
} );
@szvest ya, for folks who want to hook and and create the Schema themselves you're correct!
There's work being done by @kidunot89 to provide a WPGraphQL + WooCommerce plugin which has the Schema and (Types, Queries, Mutations and Resolvers) for all the data types in WooCommerce already scaffolded, so you could just activate that plugin and be ready to use all your WooCommerce data with GraphQL.
Fairly early in the project, but he's moving quick!
I made this last year: https://github.com/jkhaui/woocommerce-graphql
I haven't tried it since WP 5.0.0, and I haven't been following this repo for a couple of months, but it should still work fine. Feel free to submit a PR
Most helpful comment
I made this last year: https://github.com/jkhaui/woocommerce-graphql
I haven't tried it since WP 5.0.0, and I haven't been following this repo for a couple of months, but it should still work fine. Feel free to submit a PR