Webhooks is a term used for sending data to a URL when an event happens. An example is when someone subscribes to an email marketing list, the service that manages the list can send data like the subscriber’s name and email to another system like a Customer Relationship Manager. WooCommerce Webhooks were added to the WooCommerce 2.2 codebase in July of 2014.
Note: This how to is tested with WooCommerce 3.0.8 and WordPress 4.8.
Setting up WooCommerce Webhooks
To get started with webhooks, you’ll want to log in to your WooCommerce shop as an administrator. Navigate to WooCommerce > Settings and click the API tab. Click the Webhooks link in the sub menu. At first you’ll see an empty list of webhooks.
The first step is to press Add Webhook. The webhook will be added and then the settings fields will be displayed. The settings fields are described below.
This is a description only for store administrators. This can be set to whatever will help you know what the webhook is for.
Webhook status of either Active, Paused, or Disabled.
The event that will cause the webhook to fire. WooCommerce supplies 12 actions and two custom actions. The pre-defined actions are listed below.
- Coupon created
- Coupon updated
- Coupon deleted
- Customer created
- Customer updated
- Customer deleted
- Order created
- Order updated
- Order deleted
- Product created
- Product updated
- Product deleted
The two custom actions are Action and Custom.
If Action is set as the Topic, then an additional setting field named Action Event is displayed. This field will contain the WordPress hook that will fire the webhook. For example, if the site has a custom plugin with this line:
do_action( 'after_order_exported' );
Setting the Action event setting to ‘after_order_exported’ will fire the hook at the appropriate time.
If Custom is set as the Topic, then an additional setting field named Custom topic is displayed. This field will contain the name of a custom topic that has been added using this filter:
This is the URL that the webhook payload will be sent to.
The secret field is used to generate a hash of the webhook’s body and that hash is provided in the request headers. This can be used to authenticate the webhook on the receiving side.
Choose one of three available API versions. It’s okay to leave this as “WP REST API Integration v2” unless you have special circumstances.
For testing purposes, I set up my webhook with these options:
- Name: Product Created
- Status: Active
- Topic: Product created
- Delivery URL: Leave this empty for now
- Secret: a random string
- API Version: WP REST API Integration v2
The webhook can’t be saved yet, so leave this page and open a new browser window.
Test WooCommerce Webhooks
To test the WooCommerce Webhook, I’m going to use RequestBin.
Go to https://requestb.in/ and press Create a RequestBin. Optionally check the “Private” checkbox. Next, I’m taken to a page with a bunch of data. Most important is the Bin URL at the top of the page.
Copy the Bin URL to the webhook settings page in the Delivery URL field and press Save Webhook. Keep the requestb.in page open as we’ll need this after the test.
The next step is to do the action that will trigger the webhook. For this example, I’m going to create a product. To do this I navigate to Products > Add Product. I add a test product named Fuzzy Slippers, add some content, and set a price of $50. When I press Publish the product will be created and the webhook will be triggered.
After publishing the product, go back to the RequestBin window and press the Inspect button in the top right corner of the page.
You should see the connection from WooCommerce when the product was added at the top of the page. Here is the new product webhook data. The webhook headers are displayed, and the raw body which includes all of the product information in JSON format is shown.
You can also scroll down and see connections from when the Delivery URL was added and the webhook saved. WooCommerce will ping the URL to make sure it can be reached.
Using RequestBin is a fast, free, and easy way to test WooCommerce webhooks, to make sure they are working and be able to inspect the payload and see what is being delivered.