WC REST and Webhook Processes

Overview

In this section, we’ll take a look at, what–exactly happens with REST API calls and Webhook events.

For each scenario we’ll show the process, discuss the architecture a bit, and then provide an overview on some of the basic avenues developers have for customizing the behavior of each of these processes

If you prefer, you can jump to the Knowledge check to see what you already know

WC REST Process

  • WC links Authentication with the get_current_user functionThis is how WooCommerce identifies a user, without setting cookies
  • WC REST ControllersThese are located in the includes/rest-api/Controllers folder of woocommerce are at the very heart of processing the REST API request.

Rest Controllers

  • WC REST Controllers descend from the WordPress WP_Rest_Controller class and build on an abstract WC_REST_Controller classNote: there is a bit of an error in the Docs on this one, currently the class is not located in the ‘includes/abstracts folder’ but is in the includes/rest-api/Controllers/V3 folder instead
  • The Controllers are organized into V1, V2, V3 directories, and linked with namespaces accordingly
  • However, you should note, that it is not unusual for a class in Version1 to inherit properties from a foundational class in Version3
  • Controllers are linked with specific endpoints in the Server.phpfound in the includes/rest-api folder file

WC REST Family Tree

Some REST Customization

The CRUD and Post controllers allow you to customize the parameters returnedWe’ll examine this more when we connect to an MRP system for a given collection

‘woocommerce_rest_api_get_rest_namespaces’ filter allows you to assign custom ControllersWe’ll examine this more when we connect to a mobile app to endpoints

protected function get_rest_namespaces() {
  return apply_filters(
	'woocommerce_rest_api_get_rest_namespaces',
		[
		  'wc/v1' => $this->get_v1_controllers(),
		  'wc/v2' => $this->get_v2_controllers(),
		  'wc/v3' => $this->get_v3_controllers(),
		]
	);
}
To use a custom authentication methodThis may be necessary if it’s not feasible to issue an API key to each user, or if you want to use Java Web Tokens or some other Single Sign On method, hook into the ‘determine_current_user’ filter, with a priority > 15
add_filter( 'determine_current_user', 'my_authenticator_function', 20 );
return apply_filters( "rest_{$this->post_type}_collection_params", $params, $this->post_type );

The Webhook Process

It’s a long road to processing a Webhook, with each webhook loaded on WC()->init, enqueuing itself, firing, queuing, and processing the client’s response

Some Webhook Customization

The WC_Webhook_Data_Store is responsible for loading all webhooks when WC initializes. By creating a custom data store, you can implement custom logic to determine which data stores will be loaded.

return apply_filters( 'woocommerce_webhook_payload', $payload, $resource, $resource_id, $this->get_id() );

The ‘woocommerce_webhook_payload’ filter allows you to customize what dataRemember that the basis for the payload is the REST API version selected during webhook setup. is sent

do_action( 'woocommerce_webhook_delivery', $http_args, $response, $duration, $arg, $this->get_id() );

The ‘woocommerce_webhook_delivery’ action allows you to process the responseConsider using HMAC with your application’s response, then cribbing the WC_Rest_Authentication methods to authorize 😉

function wc_load_webhooks( $status = '', $limit = null ) {
	$data_store = WC_Data_Store::load( 'webhook' );
	$webhooks   = $data_store->get_webhooks_ids( $status );
	$loaded     = 0;
 ...

Conclusion

Now you have an understanding of exactly how the REST API and Webhooks interact with third party clients and a general overview of how you can customize their behavior.

Keep in mind that the brief lists of customizations are by no means comprehensiveAre they ever?. To learn more, you should have a look at the resources and inspect the code for other actions and filters available to you.

Up next we’ll look at two solid use cases on implementing the REST API and Webhooks:

  1. Integrating with a customer’s MRP system
  2. Connecting your store with a mobile App

But first, check your knowledge.

Knowledge Check

Hint, you can use the slides while working on the quiz. Use CTRL+Click to zoom into any of the diagrams for closer inspection. Then CTRL+Click to zoom back out.

References

×

Keyboard shortcuts

CTRL+Shift+F Search slideshow
F Fullscreen view
CTRL+Click Zoom in
Esc Topic overview
Right arrow,
Down arrow
Next slide
Left arrow,
Up arrow
Previous slide

Color codes

Tooltip
Hover over text more additional info
Link to an external resource
Link to an internal slide
If buttons aren't working, click in the screen to "focus" your browser
RSS
LinkedIn
Share