0

I want to subscribe an user to a Mailchimp list as soon as their account is activated. I've created a module to use hook_user_presave() and am trying to call mailchimp_subscribe_user() after that.

function mymodule_user_presave(&$edit, $account, $category) {
  if ($account->uid // user is not new
      && $account->status === "0" && $edit['status']==1) { // user is being activated
        $list_id = 1;
        $list = mailchimp_get_list($list_id);
        $mergevars = mailchimp_lists_load_user_mergevars($account, $list);

        watchdog('mymodule_log', 'account->uid '.$account->uid);
        watchdog('mymodule_log', 'list->id '.$list_id);

        mailchimp_subscribe_user($list, $account->mail, $mergevars, FALSE);

        watchdog('mymodule_log', 'got this far!');
  }
}

$account-uid has a valid value and so does $list_id. The problem that does show is up is this

Notice: Trying to get property of non-object in mailchimp_subscribe_user() 
            (line 103 of .../sites/all/modules/mailchimp/mailchimp.module).
Notice: Trying to get property of non-object in mailchimp_subscribe_user() 
            (line 105 of .../sites/all/modules/mailchimp/mailchimp.module).
Notice: Trying to get property of non-object in mailchimp_subscribe_user() 
            (line 116 of .../sites/all/modules/mailchimp/mailchimp.module).

The lines-

$double_optin = $list->settings['doublein']; //line 103

$success = $mcapi->listSubscribe($list->mc_list_id, $email, $merge_vars, 'html', $double_optin, TRUE);  //line 105

mailchimp_cache_clear_user($list->mc_list_id, $email); //line 116

And is there any better way to achieve this?

0

Try below code ... You are missing 5th argument in mailchimp_subscribe_user

function mymodule_user_presave(&$edit, $account, $category) {
  if ($account->uid // user is not new
      && $account->status === "0" && $edit['status']==1) { // user is being activated

        $mcapi = mailchimp_get_api_object();
        $list_id = 1;
        $list = mailchimp_get_list($list_id);
        $mergevars = mailchimp_lists_load_user_mergevars($account, $list);

        watchdog('mymodule_log', 'account->uid '.$account->uid);
        watchdog('mymodule_log', 'list->id '.$list_id);

        mailchimp_subscribe_user($list, $account->mail, $mergevars, TRUE, $mcapi);

        watchdog('mymodule_log', 'got this far!');
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.