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

Connect to WooCommerce plugin #321

Closed
oleksmir opened this issue Nov 21, 2017 · 5 comments
Closed

Connect to WooCommerce plugin #321

oleksmir opened this issue Nov 21, 2017 · 5 comments

Comments

@oleksmir
Copy link

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.

@jasonbahl jasonbahl reopened this Nov 27, 2017
@jasonbahl
Copy link
Collaborator

@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.

@nosovk
Copy link

nosovk commented Nov 28, 2017

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?

@szvest
Copy link

szvest commented Mar 22, 2019

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

@jasonbahl
Copy link
Collaborator

@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!

@jkhaui
Copy link

jkhaui commented Mar 22, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants