Using Conditional Fields in WooCommerce Webhooks

Webhooks were introduced in WooCommerce 2.2, and are able to trigger events any time you add, edit, or delete orders, products, coupons, or customers. This document assumes that you already are familiar with webhooks. If you are not, WooCommerce has a good guide to understand what webhooks are.

How To Create A Webhook

  1. Go to WooCommerce > Settings > Advanced > Webhooks
  2. Click the Create a new webhook or Add webhook button.
  3. Enter the requested details:
    1. Name: This is for internal reference purposes. Enter something descriptive so you know what the webhook is being used for.
    2. Status: Set to active if you want the webhook to begin delivering right away.
    3. Topic: Select what action should trigger the webhook to send data.
      1. Order Created is typically the action you will want if you are interested in retrieving conditional field data.
      2. If you are a developer, you may wish to use the Action or Custom options. For more information on those topics, please see this guide.
    4. Delivery URL: This is the URL where the webhook data will be delivered. Typically this comes from a third party website.
    5. Secret: This can be left blank unless you wish to provide a secret key value.

Conditional Checkout Field Data Included In The Webhook

When an order is sent, every conditional field you have added to your site will be sent along with the webhook. This allows you to send a test request to your delivery URL and receive all of the possible values that may ultimately be sent to the webhook. 

Only the conditional fields that a customer entered data into will have values associated with them. All other fields will simply be empty.

In the example above, the first three fields (smaller red boxes) were not filled in by the customer for this order. The fourth field (larger red box) had three values entered by the customer. Presumably because the customer purchased three of an item that triggered this field to be displayed on the checkout page.

When there is more than one entry for a particular conditional field, the data will be passed to the webhook in a comma separated list as you can see in the screenshot above.

Each conditional field will be sent in the following format:

conditional_field__[field_title]

The field title will be converted to a lowercase string with spaces replaced by underscores. So, if your field's title is "My Field Title", it will be displayed in the webhook as conditional_field__my_field_title. The values entered by the customer will follow the title.

Where Can I Find The Data That Was Passed To A Webhook?

Navigate to WooCommerce > Status > Logs. There you can select the log you wish to view.

The conditional field data will be logged in the webhook body. However, the webhook body is only logged if WP_DEBUG is enabled to avoid storing personal data in the logs.

Send Conditional Field Data To Third Party Sites

Using the built in webhook, you can send your order data to third party sites to be processed for various purposes. Send the data to a fulfilment center so your orders can be processed, or send it to Zapier to easily send the data to over 1,000 other apps like Google Sheets and many others.