Passing Marketing Permissions (GDPR) from ZC customer account to Mailchimp
In Mailchimp there is an option to enable compulsory Marketing Permissions (GDPR) checkbox(es) on a sign-up form in order to capture explicit consent to receive emails - this basically 'time-stamps' the consent for later use if needed. It would be useful to pass this explicit consent from a ZC customer account (once they have accepted the relevant GDPR privacy notice) to Mailchimps member list, so see the code below.
It is common for the explicit consent to be captured by selecting from various marketing avenues such as:
at least one of these HAS to be selected. In my case I only have one avenue (email) so the following code reflects this:
The relevant marketing permission has a 'marketing_permission_id' field which is needed and can be obtained from the output of an API call. Paste this code (updating the xxxxx) into a new php file and visit it in a browser.
Code:
<?php
$api_key = 'xxxxxxxxxxxxxxxxxxxx'; // your api key
$server = 'xxxx.'; // your server - something like 'us20', usually found at the end of the API key
$list_id = 'xxxxxxxx'; // your list id
$email = 'xxxmy_email_addressxxxxx';
$auth = base64_encode( 'user:'.$api_key );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$list_id.'/members');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$result_obj = json_decode($result);
foreach ($result_obj->member as $obj) {
echo $obj->id . ' - ' . $obj->name;
echo '<br />';
}
echo '<pre>'; print_r($result_obj); echo '</pre>';
?>
includes/functions/extra_functions/mailchimp.php
Code:
public function list_add_subscriber(Array $options)
{
// Check required params
if ( ! isset($options['id_list']) OR ! isset($options['email']))
{
throw new MailChimpException('Parameters not set on '.__METHOD__);
}
$endpoint = '/lists/' . $options['id_list'] . '/members/';
$payload = [
'email_address' => $options['email'],
'status' => 'subscribed',
'marketing_permissions' => array([
'marketing_permission_id' => 'xxxxxxxxx',
'enabled' => true
])
// 'status' => isset($options['status']) ? $options['status'] : 'pending',
];
if (isset($options['merge_fields']) AND $options['merge_fields'])
{
$payload['merge_fields'] = $options['merge_fields'];
}
return $this->_execute_request('POST', $endpoint, $payload);
}
There may be a tidier way to do it, that's all I could manage.
This now passes the marketing permission consent from a customers ZC account to Mailchimps members list.
Bookmarks