Mailchimp Webhook Integration – a WooCommerce Example
In this tutorial we’re going to talk about Mailchimp webhooks integration because I’ve been working with them a lot, both with clients and when developing my Mailchimp plugin for WordPress.
Webhooks basically allow you to send information about subscribers in your audience to your website or an app. As an example, we’re going to take a look at an eCommerce (WooCommerce) store over here.
Create a Mailchimp Webhook
First thing first, we need to go to our Mailchimp account and create a webhook there. As I already said before, you can do it for a specific audience in your account. If you want to do it for multiple audiences, you will need to create multiple webhooks.
Let’s take a look at two ways of creating a Mailchimp webhook.
How to create a webhook URL on your server?
But first, before creating a webhook, you should know one thing – the URL of the webhook must exist on your server and should be able to accept POST requests.
This is a necessary part of Mailchimp webhooks integration. It could be a separate PHP file or if you’re working with WordPress, you can process webhooks in your functions.php file. For example, if your webhook URL on a WordPress website looks like this: https://your-website.com/?process-webhook=yes, you can process it in the functions.php with the code below:
add_action( 'init', 'misha_process_webhook' );
function misha_process_webhook(){
if( isset( $_GET[ 'process-webhook' ] ) && 'yes' === $_GET[ 'process-webhook' ] ) {
// do some stuff
// you can send $_POST params by email to debug it for example
}
}If you are not working with WordPress at this moment, just create a custom PHP file on your server. We are going to talk about it in detail below, but now let’s continue to create a webhook.
Creating a webhook manually
The easiest one, but not always convenient. In your Mailchimp account go to Audiences and select one. Open the Settings dropdown menu and go to Webhooks.

Hit the “Create New Webhook” button and the following page should appear:

I think everything is pretty intuitive in the screenshot above, so let’s continue to the second way. The only thing, the Callback URL is a page or a file on your website which is going to process the webhook, we’re going to talk about it later.
Creating a webhook with API
But when you’re working with client websites or creating a custom plugin for WordPress or whatever, it is not always convenient to request Mailchimp account access or to ask clients to make all the changes above.
That’s why sometimes you have to create webhooks with the Mailchimp API. And you can do it!
In all the code listings below you will need two variables:
$api_key– it is required to connect to Mailchimp API, read here how to get it.$list_id– this is the step-by-step how to get it.
Since I’ve been working with WordPress mostly, in the code below I am using WordPress HTTP API functions like wp_remote_post().
$api_key = '';
$list_id = '';
$response = wp_remote_post(
'https://' . substr( $api_key, strpos( $api_key, '-' ) + 1 ) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/webhooks/',
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
),
'body' => json_encode( array(
'url' => 'https://your-website/?process-webhook=yes',
'events' => array(
'subscribe' => true, // when someone subscribes to a list
'unsubscribe' => false, // when someone unsubscribes from a list
'profile' => false, // subscriber's profile is updated
'cleaned' => false, // subscriber's status is cleaned
//'upemail' => false, // subscriber's email address is changed
//'campaign' => false // campaign is sent or cancelled
),
'sources' => array(
'user' => true, // changes are initiated by subscriber himself
'admin' => true, // admin-initiated actions in MailChimp dashboard
'api' => false // changes made via API
)
) ),
)
);You can read more about Mailchimp API in the official documentation.
After sending a request you can get a response with the wp_remote_retrieve_body() function. It is going to be a JSON-formatted string, you can json_decode() as well then.
The response parameters are going to be:
| Response parameter | Description |
|---|---|
id | The webhook ID |
url | The webhook URL |
events | An object with boolean values for each event, example $response_body->events->subscribe is true. |
sources | An object with boolean values for each source, example $response_body->sources->api is false. |
list_id | Mailchimp list ID the webhook has been created for. |
But, of course, it is not necessary to use WordPress functions in order to create a Mailchimp webhook and code integration for it. For example, it can be done with cURL this way:
$api_key = '';
$list_id = '';
$connection = curl_init();
curl_setopt_array( $connection, array( // PHP 5.1.3+
CURLOPT_URL => 'https://' . substr( $api_key, strpos( $api_key, '-' ) + 1 ) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/webhooks/',
CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(array(
'url' => 'https://your-website.com/?process-webhook=yes',
'events' => array(
'subscribe' => true,
'unsubscribe' => false,
'profile' => false,
'cleaned' => false,
//'upemail' => false,
//'campaign' => false
),
'sources' => array(
'user' => true,
'admin' => true,
'api' => false
)
))
));
$response_body = json_decode( curl_exec( $connection ) );
curl_close( $connection );Mailchimp Webhook Integration with a WooCommerce Store
Ok, now it is time to process a webhook on our server. How we can do that?
First of all, the Mailchimp webhook we created above will send specific data to our WooCommerce store each time a user is subscribed to our Mailchimp audience. As an example, let’s create a coupon programmatically for this specific user and send it by email.
Sounds interesting?
Below is just the code which you can insert into your theme functions.php file or whatever.
/**
* @snippet Mailchimp webhook integration
* @author Misha Rudrastyh
* @url https://rudrastyh.com/mailchimp-api/webhooks.html
*/
add_action( 'init', function() {
// do nothing if we're not processing the webhook right now
if( empty( $_GET[ 'process-webhook' ] ) || 'yes' !== $_GET[ 'process-webhook' ] ) {
return;
}
// here Mailchimp send POST-data about the event, we can also check that
if( 'POST' !== $_SERVER[ 'REQUEST_METHOD' ] ) {
return;
}
if( 'subscribe' === $_POST[ 'type' ] && $_POST[ 'data' ][ 'email' ] ) {
// probably it is not a good idea, but just an example :)
$coupon_code = 'thanks-for-subscribe' . rand( 10000, 50000 );
// creating a coupon
$coupon = new WC_Coupon();
$coupon->set_code( $coupon_code );
$coupon->set_amount( 200 );
$coupon->save();
// sending an email
wp_mail(
$_POST[ 'data' ][ 'email' ],
'Thank you for subscription, here is your discount!',
"You can use this discount code: {$coupon_code}"
);
}
} );We can also make updates in this code in case a user is unsubscribed from the audience, we can remove the user-specific coupon:
if( 'unsubscribe' === $_POST[ 'type' ] && $_POST[ 'data' ][ 'email' ] ) {
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
Nice post :)