Keeping your email marketing lists up-to-date is essential for effective communication with your customers. Integrating WooCommerce with Mailchimp ensures that any changes to customer data—like updated email addresses, names, or preferences—are automatically synced to your email list. In this blog post, we’ll show you how to use WooCommerce webhooks to send updated customer data to Mailchimp.
Why Sync Customer Data with Mailchimp?
Having accurate and up-to-date customer information in Mailchimp allows you to:
- Send personalized email campaigns.
- Segment your audience effectively.
- Improve email deliverability by avoiding outdated email addresses.
Using WooCommerce webhooks, you can automate this process, ensuring your Mailchimp list stays in sync with changes made in WooCommerce.
Prerequisites
Before we start, make sure you have:
- WooCommerce Installed: Your WooCommerce store should be active and functional.
- Mailchimp Account: An active Mailchimp account with API access.
- Mailchimp Audience: Ensure you have an audience (list) set up to receive customer data.
- API Key: Generate an API key in Mailchimp for integration.
- Admin Access: Access to your WordPress admin dashboard.
Step 1: Set Up a Webhook in WooCommerce
- Log in to your WordPress admin dashboard.
- Navigate to WooCommerce > Settings > Advanced > Webhooks.
- Click Add Webhook to create a new webhook.
- Configure the webhook:
- Name: Enter a descriptive name, such as “Customer Update to Mailchimp.”
- Status: Set to Active.
- Topic: Choose Customer Updated (or Customer Created if you also want to sync new customers).
- Delivery URL: Enter the URL of the middleware or script that will process the webhook and send data to Mailchimp.
- Secret: (Optional) Add a secret key for security.
- Click Save Webhook.
Step 2: Generate a Mailchimp API Key
- Log in to your Mailchimp account.
- Click your profile icon and go to Account > Extras > API Keys.
- Click Create A Key and copy the API key.
Step 3: Create Middleware or Use an Integration Tool
To handle the data sent by WooCommerce and forward it to Mailchimp, you can either use a no-code tool like Zapier or write your own custom script.
Option 1: Use Zapier for Integration
- Log in to Zapier and create a new Zap.
- Set WooCommerce as the trigger app with the Customer Updated event.
- Authenticate your WooCommerce account.
- Add Mailchimp as the action app.
- Select Add/Update Subscriber as the action.
- Map WooCommerce customer fields (e.g., name, email, address) to Mailchimp fields.
- Test the Zap to ensure it works.
Option 2: Write a Custom PHP Script
If you prefer to handle the integration manually, create a script to receive the WooCommerce webhook data and update Mailchimp using their API.
Here’s an example PHP script:
<?php
// Mailchimp API credentials
$api_key = 'YOUR_MAILCHIMP_API_KEY';
$list_id = 'YOUR_AUDIENCE_ID';
$api_url = "https://<dc>.api.mailchimp.com/3.0/lists/$list_id/members/"; // Replace <dc> with your data center (e.g., us1, us2)
// Function to handle WooCommerce webhook data
function handle_webhook($webhook_data) {
$email = strtolower($webhook_data['email']); // Mailchimp requires lowercase emails
$first_name = $webhook_data['first_name'];
$last_name = $webhook_data['last_name'];
// Prepare subscriber data
$subscriber_data = [
'email_address' => $email,
'status_if_new' => 'subscribed', // Ensure the user is subscribed if they don't exist
'merge_fields' => [
'FNAME' => $first_name,
'LNAME' => $last_name
]
];
// Update Mailchimp
send_to_mailchimp($email, $subscriber_data);
}
// Function to send data to Mailchimp
function send_to_mailchimp($email, $data) {
global $api_key, $api_url;
// Hash the email for Mailchimp API
$subscriber_hash = md5($email);
$url = $api_url . $subscriber_hash;
$headers = [
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json'
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // Mailchimp uses PUT for add/update
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
?>
Step 4: Test the Integration
- Update a customer’s details in WooCommerce (e.g., their email or name).
- Verify that the webhook is triggered and sends data to your middleware or script.
- Check Mailchimp to confirm the customer’s information has been updated.
Step 5: Monitor and Optimize
- Monitor WooCommerce webhook logs to ensure data is being sent without errors.
- Periodically test updates to ensure changes in WooCommerce are reflected in Mailchimp.
- Expand the script to include additional fields, like phone numbers or custom tags.
Conclusion
Syncing WooCommerce customer data with Mailchimp ensures your email marketing list is always accurate and ready for targeted campaigns. Whether you use a no-code tool like Zapier or write your own custom script, this integration is an investment in better customer communication and engagement.
If you have questions or additional tips, feel free to share them in the comments below!
Photo by Gwendal Bar on Unsplash