Hello. I'm trying to have Drupal send an update to MailChimp every time the profile or account is updated to keep the mailchimp information as fresh as possible since we send quite a few mailchimp campaigns. Some of our merge fields are exclusive to the user, like a bonus link that hashes certain parts of their profile that may change. The problem I'm encountering seems to me in MailChimps loading of mergevars.

With the current way that I've built this, and also every previous version I've tried using hook_form_alter() and running user_load() from the form_state, when I save the form, it updates mailchimp with the previous info. If I change my name from One to Two and save, MailChimp updates to One. When I change my name from Two to Three and save, MailChimp updates to Two, and so on.

What would be the easiest way to get the latest version of the mergevariables? Using dsm() to view all of the things I'm loading here, I see that the $mergevars are out of date. They are what the values were previous to saving the form. Should I be building the mergevars manually from the profile information? I would think that the mailchimp_lists_load_user_mergevars() function would grab the latest version, but it seems to not be doing that.

I would like to get this working, and I can build out the mergevars array manually, but I'm just wondering if I'm missing something simple with the order I'm doing things in, or if there is some sort of bug in mailchimp_lists_load_user_mergevars() - highly unlikely but who knows - that is grabbing an older version of the user information.

My code for this is as follows (yes I know you use your module name instead of hook):

First Iteration:

<?php
function hook_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
 
$form['#submit'][] = 'custom_function_that_updates_mailchimp';

  return
$form;
}

function
custom_function_that_updates_mailchimp($form, &$form_state) {
 
$account = user_load($form_state['profiles']['user_profile']->uid, TRUE);
 
$mcapi = mailchimp_get_api_object();
 
$list = current(mailchimp_lists_get_available_lists($account));
 
$mergevars = mailchimp_lists_load_user_mergevars($account, $list);
 
mailchimp_update_user($list, $account->mail, $mergevars, TRUE, $mcapi);
}
?>

Current version:

<?php
function hook_user_update(&$edit, $account, $category) {
 
//This is redundant as the $account object already exists, I just figured I'd try to reset cache and load it from the database to see if I could get the newer information
 
$account = user_load($account->uid, TRUE);

 
//generate an api object
 
$mcapi = mailchimp_get_api_object();

 
//grab the list we're trying to update the users info for.
 
$list = current(mailchimp_lists_get_available_lists($account));

 
//grab the mergevars for the list and user we're updating
 
mailchimp_cache_clear_user($list->mc_list_id, $account->mail);
 
$mergevars = mailchimp_lists_load_user_mergevars($account, $list);

 
//if user is subscribed to mailchimp list, update users info in mailchimp
 
if (mailchimp_is_subscribed($list->mc_list_id, $account->mail)) {
   
mailchimp_flush_caches();
   
mailchimp_update_user($list, $account->mail, $mergevars, FALSE, $mcapi);
  }
}
?>

Any help at all would be wonderful! Thanks.

Comments

mike.roberts’s picture

Turns out the issue was with profile2. Hopefully my time spent on this will help someone in the future, or maybe we could get this functionality actually rolled into the MailChimp Module perhaps? It's actually a pretty useful feature and would take a load off of the whole running on cron and "queue existing" functionality that currently happens.

Using hook_profile2_update worked like so:

<?php
function hook_profile2_update($profile) {
 
//clear out the mailchimp cache in Drupal - not sure if this is necessary, just a precaution.
 
mailchimp_flush_caches();
 
//load the user object
 
$account = user_load($profile->uid, TRUE);
 
//generate mailchimp api object
 
$mcapi = mailchimp_get_api_object();
 
//grab the list we're trying to update the users info for.
 
$list = current(mailchimp_lists_get_available_lists($account));
 
//grab the mergevars for the list we're updating
 
$mergevars = mailchimp_lists_load_user_mergevars($account, $list);
 
//if user is subscribed to mailchimp list, update users info in mailchimp
 
if (mailchimp_is_subscribed($list->mc_list_id, $account->mail)) {
   
mailchimp_update_user($list, $account->mail, $mergevars, FALSE, $mcapi);
  }
}
?>

Still working on what it would be for the regular account since neither hook_profile2_update or hook_user_update are working for that. I'll update this if I find a solution for that before someone else does.

mike.roberts’s picture

Status:Active» Closed (fixed)
mike.roberts’s picture

Issue summary:View changes

Changed to from code to PHP for syntax highlighting