How to Work with WooCommerce Webhooks Programmatically

In this tutorial, I am going to talk about the webhooks functionality, which you can find in WooCommerce settings, Settings > Advanced > Webhooks. But I am not going to show you how to manage webhooks via the WooCommerce admin; only code examples will be below.

For example, let’s say you created a couple of webhooks for “Order Created” and “Order Updated” topics, and your goal now is to manage orders programmatically and send appropriate webhooks. The thing here is that these webhooks are not going to be triggered automatically on woocommerce_update_order or woocommerce_new_order action hooks.

So, what can we do in this case?

I also had this question when working with my Multisite Order Sync for WooCommerce plugin, because the customer wanted the webhooks functionality. But, as we already know, they will not be triggered by default when orders are created programmatically.

Basically, there are two ways to trigger a specific webhook, and we need to know some information about it:

  • The webhook ID,
  • The topic (in our case, order.created and order.updated),
  • The order ID.

Now, let’s talk about it step by step.

How to Get WooCommerce Webhooks Programmatically

As I mentioned above, to send a WooCommerce webhook, we need to know its ID. For example, let’s say we’re updating a specific order (using Multisite Order Sync for WooCommerce or just manually in the code). Only this can provide us with some information:

  • We definitely know an order ID we’re updating
  • The webhook topic will be order.updated (topics are displayed in a separate table column on the Settings > Advanced > Webhooks page).
  • The webhook status will be active (there are also paused and disabled statuses).

I didn’t find a proper function to get WooCommerce webhooks, so I decided to create one myself. The webhooks in WooCommerce are stored in a separate database table, and the easiest way to get them is to send an SQL query using $wpdb. It is super-convenient; however, it bypasses any cache.

function rudr_get_webhooks( $topic, $status = 'active' ) {
	
	global $wpdb;

	return $wpdb->get_col( 
		$wpdb->prepare(
    		"
			SELECT webhook_id FROM {$wpdb->prefix}wc_webhooks 
			WHERE topic = %s
			AND status = %s
			",
			$topic,
			$status
		)
	);

);

After that, you can get all webhook IDs of a specific topic like this:

$webhook_ids = rudr_get_webhooks( 'order.updated' );
// Array( 1, 2, 5 )

Delivering Webhooks Programmatically

I think, at this point, we already have everything we need to trigger a webhook delivery programmatically. There are two ways to do that:

  • to trigger an instant delivery,
  • to trigger a delivery in the background.

We are going to take a look at both of these methods.

Method 1. Instant webhook delivery

This method is super-simple; however, I would not recommend using it often, because, if you have a lot of webhooks for a specific order (in our case), it may significantly slow down order processing.

$order_id = 12345;
$webhook_ids = rudr_get_webhooks( 'order.updated' );

foreach( $webhook_ids as $id ) {
	$webhook = wc_get_webhook( $id );
	if( $webhook ) {
		$webhook->deliver( $order_id );
	}
}

Method 2. Asynchronous webhook delivery in the background (with the help of Action Scheduler)

In most cases, I would recommend a more optimal way of delivering WooCommerce webhooks programmatically – asynchronously, it can be done the following way:

$order_id = 12345;
$webhook_ids = rudr_get_webhooks( 'order.updated' );

foreach( $webhook_ids as $id ) {
	WC()->queue()->add( 
		'woocommerce_deliver_webhook_async', 
		array(
			'webhook_id' => $id,
			'arg'        => $order_id,
		), 
		'woocommerce-webhooks'
	);
}

Our webhook is now scheduled for delivery:

WooCommerce webhooks delivery using Action Scheduler
WooCommerce > Status > Scheduled Actions
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X