How To Test WooCommerce Webhooks

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.

In this article, you’ll learn how to add and update WooCommerce Webhooks with these

  1. Add WooCommerce Webhook
  2. Test WooCommerce Webhook

But fist, let’s discuss some ways that webhooks can be used.

How can webhooks be used?

With today’s integrated services and the no-code movement webhooks provide a way to share updated data with an external service after an event. This could include:

  • An order is placed and needs to be fulfilled
  • A customer registers on a site and needs to be added to a CRM
  • A product is added and needs to be included in a social media content calendar
  • A customer updates the shipping address on an order and the new address needs to be sent to a 3PL

As a bit of history WooCommerce Webhooks were added to the WooCommerce 2.2 codebase in July of 2014.

Note: This example is tested with WooCommerce 5.3 and WordPress 5.7.

Add 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 Advanced tab. Click the Webhooks link in the sub menu. At first you’ll see an empty list of webhooks.

There are no webhooks currently added to the store.

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.

Name

This is a description only for store administrators. This can be set to whatever will help you know what the webhook is for.

Status

Webhook status of either Active, Paused, or Disabled.

Topic

The event that will cause the webhook to fire. WooCommerce supplies 15 actions and two custom actions. The pre-defined actions are listed below.

  1. Coupon created
  2. Coupon updated
  3. Coupon deleted
  4. Coupon restored
  5. Customer created
  6. Customer updated
  7. Customer deleted
  8. Order created
  9. Order updated
  10. Order deleted
  11. Order restored
  12. Product created
  13. Product updated
  14. Product deleted
  15. Product restored

To use a custom action select Action.

Action event

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.

Custom

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:

woocommerce_webhook_topic_hooks

Delivery URL

This is the URL that the webhook payload will be sent to. You can request this URL from the provier who will

Secret

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.

API Version

Choose one of three available API versions. It’s okay to leave this as “WP REST API Integration v3” unless you have special circumstances.

For testing purposes, I set up my webhook with these options:

  • Name: Product Created
  • Status: Active
  • Topic: Product updated
  • Delivery URL: Leave this empty for now
  • Secret: a random string
  • API Version: WP REST API Integration v3

The webhook can’t be saved yet, so leave this page and open a new browser window.

Test WooCommerce Webhooks

To test your WooCommerce webhook you will need either a free website that will receive the webhook request, or build a custom app. If you want to go the built it yourself route here is a package for receiving webhooks in a Laravel app.

Important: If you choose to use a free app be sure to read and understand the site’s privacy policy and never test with customer data.

For this walk through I’m going to use Webhook.site (privacy policy).

Opening the webhooks site will give you a unique URL for the webhook:

In the Your unique URL section click Copy to clipboard.

Paste the URL to the webhook settings page in the Delivery URL field and press Save Webhook. Keep the webhooks 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 then published the product. When I press Update after changing the product it will be updated and the webhook will be triggered.

After publishing and updating the product, go back to the webhooks window. You should see the connection from WooCommerce as a POST with full details of the webhook.

The content is the product’s data in json format.

Final thoughts

Webhooks are a very useful way to integrate your WooCommerce store with external systems. Using webhook.site 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 data is being delivered.

Got any questions? Ask below, or hit up Grow Development on Twitter!

Daniel Espinoza

Daniel has been building ecommerce plugins and sites since 2007. He built the first commercial plugin for WooCommerce in 2011 and has built dozens of plugins and sites since.

10 Comments

  • GuardianOfTheBucket 4 years ago Reply

    Where is the secret in the last image ?

  • david 4 years ago Reply

    Is there a way to get WooCommerce to call a webhook when the customer pays and INCLUDE ALL THE INFORMATION ABOUT THE TRANSACTION? The above will send a message {“action”:”woocommerce_payment_complete_order_status”,”arg”:”processing”}. With all due respect while this is technically correct information, it lacks the needed detail to be classified as useful.

    Many questions remain about the transaction that include:
    1) did the payment go through?
    2) what did the customer buy?
    3) how much did the customer pay?
    4) customer contact information?

    PayPal does this through their IPN. Is there an equivalent function in WooCommerce. Not interested in a “plugin”. The more moving parts the less reliable. Native WooCommerce only.

    It is clear that the WooCommerce webhooks do all sorts of very fancy things like notify when a product is added or a customer was deleted. But can WooCommerce webhooks tell another system that, 100% for sure, the payment was processed, what was purchased, and by whom? And If so can you please detail, step by step, what has to be done on the WooCommerce end to make it send messages that contain all the transaction data?

  • James 4 years ago Reply

    Is it possible to have webhooks with a condition? For example if the cart has $0 balance then call the webhook.

    • Daniel 4 years ago Reply

      Hi James,

      You would need to define your own web hook and include the logic to test the cart for $0 before firing it.

  • Ricardo Ardila 4 years ago Reply

    Hi Daniel,
    Is it possible that the json body of the request contain also the url of the purchased product like:

    “line_items”: [
    {
    “id”: 40,
    “name”: “Woo Single #2”,
    “image_url”: “http:mywoo/wp-content/uploads/…/product40.jpg”,
    “product_id”: 99,
    “variation_id”: 0,
    “quantity”: 1,
    “tax_class”: “”,
    “subtotal”: “0.00”,
    “subtotal_tax”: “0.00”,
    “total”: “0.00”,
    “total_tax”: “0.00”,
    “taxes”: [],
    “meta_data”: [],
    “sku”: “”,
    “price”: 0
    }],
    “tax_lines”: [],
    “shipping_lines”: [],
    “fee_lines”: [],
    “coupon_lines”: [],
    “refunds”: []
    }

    Thanks

  • John Axne 4 years ago Reply

    Thanks for posting this article Daniel!
    https://requestb.in/ worked great for us to test with.

    I did have a question to the you and the group related to when Woocommerce sends the payload of the webhook. We have webhooks set up on 8 different woocommerce stores that send customer and order information into a data feed processor for SalesForce. It’s set to the “Order.Created’ action in WooCommerce.

    Today we had a scenario where a customer tried placing an order but payment failed. (That webhook payload was sent as a failed transaction as Woo did create the order to trigger the webhook). The customer then fixed the payment issue and the payment went through. However, Woocommerce did not send a 2nd payload with the successful transaction information. The reason I’m assuming is that the order was already created so it didn’t trigger the Order.Created webhook action. In order to have WooCommerce send the send the 2nd successful transaction, I’m assuming we’d need to set up a 2nd webhook with the Order.Updated action. Correct? Has anyone run into this? Just thought I’d ask.

    Thanks!

  • Vane 4 years ago Reply

    Hi,
    I can’t debug my Woocommerce webhook, I hope someone of you can help.
    I tested both
    Product updated
    and
    Customer created
    Wooc. webhooks, completed the trigger events many times, Requestb.in light (in right top corner) turns green in both cases, but the Raw body area only shows
    webhook_id=xxxx
    and nothing else.
    (Your sample image at Raw body section shows the values in JSON wrote with green; my webhook ID is black and nothing else there.
    The problem exists with both latest WP (4.9) and Woocommerce (3.2.4) versions and with the previous ones too.
    What might be wrong or missing?

    Thanks!

Have a question? Ask it below and we will try to help!

Important:
  • Write all questions in English.
  • Please wrap any code with [php] [/php]
  • Be kind!