The issue right now is when you save your profile fields, it syncs the previous values with mailchimp which means you essentially have to save the user page twice. I patched it with 'after_update' and it works just fine.

CommentFileSizeAuthor
#5 mailchimp.module.patch1.61 KBkanani
#4 mailchimp.module.patch1.25 KBkanani
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

levelos’s picture

kanani’s picture

Status: Closed (fixed) » Active

So changing hook_user to fire on 'after_update' causes an issue when a user is trying to change their email address.

On 'update' the old email address is preserved in $account->mail and the new email is stored in $edit['mail'] so that listUpdateMember is called, mailchimp has a valid key to work against to pass in the new email in the merge_vars.

If hook_user is fired on 'after_update', $account->mail and $edit['mail'] both contain the new email so instead of the old getting set to new, a new subscriber is added to the list in mailchimp, and the old one is retained.

In addition to having the user subscribed twice (albeit with different email addresses), this makes it impossible to delete the original email from mailchimp through Drupal because any calls to listUnsubscribe will use the old email as a key.

Maybe a workaround to this would be to have fire hook_user on 'update' but only let it update mail, then the rest of the updates can occur on 'after_update'?

kanani’s picture

if($is_allowed && $op == 'update') {
              if($account->mail != $edit['mail']) {
                $userinfo['EMAIL'] = $edit['mail'];
                $ret = _mailchimp_subscribe_user($list, $account->mail, $userinfo, FALSE, $q);
              }
            }
kanani’s picture

Status: Active » Needs review
FileSize
1.25 KB

Patch to check email on update, everything else on after_update. Also make sense to revisit whether this should fire on $op == 'update'

--- sites/default/modules/contrib/mailchimp/mailchimp.module.orig	2011-07-25 11:11:43.000000000 -0700
+++ sites/default/modules/contrib/mailchimp/mailchimp.module	2011-07-25 11:30:14.000000000 -0700
@@ -68,7 +68,7 @@
     }    
   }
 
-  if (in_array($op, array('insert', 'delete', 'after_update')) && $q = _mailchimp_get_api_object()) {
+  if (in_array($op, array('insert', 'delete', 'update', 'after_update')) && $q = _mailchimp_get_api_object()) {
     foreach ((array)_mailchimp_get_required_lists() as $list) {
       $action_taken = FALSE;
       switch ($op) {
@@ -80,6 +80,7 @@
           break;
         // insert or update a user to/in a MC list
         case 'insert':
+        case 'update':
         case 'after_update':
           // don't repeat if already managing via cron
           if (!variable_get('mailchimp_cron', FALSE) ) {
@@ -94,9 +95,15 @@
                 break;
               }
             }
-
+            if($is_allowed && $op == 'update') {
+              if($account->mail != $edit['mail']) {
+                //update the users email address then break
+                $userinfo['EMAIL'] = $edit['mail'];
+                $ret = _mailchimp_subscribe_user($list, $account->mail, $userinfo, FALSE, $q);
+              }
+            }
             // they are allowed, update or subscribe
-            if($is_allowed){
+            elseif($is_allowed){
               $userinfo = _mailchimp_load_user_list_mergevars($account->uid, $list->id, $q->listMergeVars($list->id));
               $userinfo['EMAIL'] = $edit['mail'];
               $ret = _mailchimp_subscribe_user($list, $account->mail, $userinfo, FALSE, $q);

kanani’s picture

FileSize
1.61 KB

Previous patch didn't include 'update' check in if in_array check.

bleedev’s picture

Component: Code » General
Status: Needs review » Closed (fixed)

Committed change to pass updated email to to use in 'after_update'.