In this lunchtime #WPQuickies, I talk about how to set up and use webhooks ↪ with WooCommerce.
What Are Webhooks And Why Use Them?
Webhooks allow disparate applications to share information.
There are generally two ways for apps to communicate, polling and webhooks.
Polling is where you ask an app if it has any information to offer, similar to asking your friends if they would like another round of drinks and which ones, in a pub.
Webhooks simply give information to an app on each occurrence, so in the analogy above you simply bring a round of drink to your friends when it’s your turn, no questions asked.
Webhooks allow you to automate processes and automation ultimately saves time and money, when implemented properly, so that’s a good thing to do!
Let’s have a look at what we can do with WooCommerce webhooks.
Webhooks were introduced in WooCommerce 2.2 and can trigger events each time you add, edit or delete orders, products, coupons or customers.
What makes it even more powerful is that you can trigger a webhook on an action
e.g. Create a webhook to be used every time a product is added to the shopping cart, using the action woocommerce_add_to_cartYou can find a complete list of WooCommerce event actions here: https://woocommerce.github.io/code-reference/hooks/hooks.html
WooCommerce Webhook Add To Cart
Let’s set up a webhook to fire when somebody adds something to the cart.
We can use a site called https://webhook.site/ to test the hook and see what data it transmits.
I added the test URL supplied from the webhook.site and the secret was filled in automagically when I saved the webhook.
Here’s the full response we got from the webhook firing.
We’re only really interested about the data being given to us – that’s the raw content section
Here we can see that the payload data has the action hook name and the argument is the cart item key.
You could theoretically get all the item details using the WC_Cart class and the get_cart_item() method, or using the REST API.
WooCommerce + Webhook + Zapier
The real power comes when you start to automate with Zapier.
Catching webhooks is a premium service so you’ll have to sign up for that before you can use custom hooks.
Then you can set your zap to do all sorts of stuff – send a notification to a Slack channel, add a person onto a CRM etc.
So, you don’t have to build your own service to catch a webhook, you could easily use zapier as the middleware to funnel data to the thousands of integrations it offers.
Here’s a link to more information on Zapier webhooks https://zapier.com/help/create/code-webhooks/trigger-zaps-from-webhooks
Of course, Zapier has its own WooCommerce “recipes” so you don’t have to do through all the hoops of using a custom webhook and it’s not just WooCommerce that offers webhooks, many plugins and other services allow you to trigger a webhook.
I use Active Campaign as my CRM and it allows you to trigger webhooks for heaps of different events.
I thought it would be helpful seeing webhooks in action and maybe that will spur a thought or two about how you can automate processes in your business workflow.
Having Trouble Removing Action Hooks and Filters?
Removing WordPress action hooks and filters put in place by other plugins or themes using Object-Oriented (OO) code can be tricky.
Fortunately I have a few ways you can use to try to remove them.
Read my post How To Remove an Action Hook or Filter Added By a Class in WordPress.
Join me every Thursday at 1 pm AEST for some more WPQuickies – WordPress tips and tricks in thirty minutes or less.
Broadcasting live on YouTube and Facebook.
Suggest a #WPQuickies Topic
If you have an WordPress topic you’d like to see explained in 30 mins or under, fill out the form below.