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
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(),
]
);
}
add_filter( 'determine_current_user', 'my_authenticator_function', 20 );
return apply_filters( "rest_{$this->post_type}_collection_params", $params, $this->post_type );
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
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;
...
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:
But first, check your knowledge.
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.
0 of 5 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 5 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
How does the REST API map authentication to a registered WP user?
Where is the user verified as authorized to perform the requested action? (choose the BEST answer, as each of these is a little correct)
Which of the following descend from the WC_REST_Posts_Controller?
What class queries webhooks from the database on WC()->init()?
What happens after the Client sends the reply to the Webhook?