
Originally Posted by
simon1066
ZC v1.5.7c
PHP v7.3
I've updated my mailchimp.php with the patch for PATCH from @swguy's
github contribution. I'm guessing that the issue lies somewhere in the other three edited files.
I've had to give up on being able to have changes of email address in ZC updated on Mailchimp's member's list (although I've confirmed that PATCH is functioning I'm unable to get the change of email to work). I thought I would post my documentation on this tweak to @swguy's mod.
Mailchimp allows for a list of subscribers to be segregated (into Groups) based on the selection made when completing the sign-up form, such as:
This is useful for sending different newsletters to different subscribers. However, if a subscriber signs up for newsletters on a ZC website, no groups are transferred to Mailchimp - meaning those subscribers will be missed if solely relying on groups to send newsletters.
Until I manage (not guaranteed) to add the relevant checkboxes to the account_newsletters page I have assumed that a subscriber on my website will automatically be signed up for both groups (wording the descriptive text on the signup page accordingly). To achieve this here are the relevant code additions (in red):
includes/languages/english/extra_definitions/mailchimp_sidebox_defines.php
Code:
define('BOX_MAILCHIMP_NEWSLETTER_ID','xxxxxxxxxx');
define('BOX_MAILCHIMP_NEWSLETTER_U','yyyyyyyyyy');
define('BOX_MAILCHIMP_NEWSLETTER_URL','http://YourSite.us1.list-manage.com/subscribe/post');
define('BOX_MAILCHIMP_NEWSLETTER_API_KEY','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('MAILCHIMP_INTEREST_ID_A','aaaaaaaaaaa');
define('MAILCHIMP_INTEREST_ID_B','bbbbbbbbbbb');
// Language defines for newsletter sidebox
define('BOX_HEADING_MAILCHIMP_SIDEBOX', 'Newsletter');
define('BOX_MAILCHIMP_PITCH', 'Subscribe to our newsletter for periodic updates and valuable coupons.');
define('BOX_MAILCHIMP_SUBSCRIBE', 'Subscribe');
includes/functions/extra_functions/mailchimp_functions.php
Code:
function mailchimp_add($email) {
$mailchimp = new MailChimp(BOX_MAILCHIMP_NEWSLETTER_API_KEY);
$reply = $mailchimp->list_add_subscriber([
'id_list' => BOX_MAILCHIMP_NEWSLETTER_ID,
'interest_id_A' => MAILCHIMP_INTEREST_ID_A,
'interest_id_B' => MAILCHIMP_INTEREST_ID_B,
'email' => $email,
'status' => 'pending'
]);
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',
'interests' => array(
$options['interest_id_A'] => true,
$options['interest_id_B'] => 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);
}
Notes: Groups in Mailchimp dev talk are 'interests'. Finding the appropriate id of an interest (to add to the language file) is not straightforward. These days the only way to find these out is via an API call. Add the following code to a new php file in the root directory of your website and run it in a browser e.g. mywebsite[dot]com/mynewfile.php. The code is on this page, but I'll include it here in case it disappears.
Code:
<?php
$api_key = 'xxxxxxxxxxxxxxxxxxx-us13'; // your api key
$server = 'us13.'; // your server
$list_id = 'xxxxxxxxxxxxx'; // your list id
$group_id = 'xxxxxxxxxxxx'; // your group id
$auth = base64_encode( 'user:'.$api_key );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$list_id.'/interest-categories/'.$group_id.'/interests');
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->interests as $obj) {
echo $obj->id . ' - ' . $obj->name;
echo '<br />';
}
echo '<pre>'; print_r($result_obj); echo '</pre>';
?>
Bookmarks