You are here

function mailchimp_batch_update_members in Mailchimp 7.4

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_batch_update_members()
  2. 7.5 mailchimp.module \mailchimp_batch_update_members()
  3. 7.3 mailchimp.module \mailchimp_batch_update_members()
  4. 2.x mailchimp.module \mailchimp_batch_update_members()

Wrapper around MailchimpLists->addOrUpdateMember().

$batch is an array where each element is an array formatted thus: 'email' => 'example@example.com', 'email_type' => 'html' or 'text', 'merge_vars' => array('MERGEKEY' => 'value', 'MERGEKEY2' => 'value2'),

1 call to mailchimp_batch_update_members()
mailchimp_lists_execute_mergevar_batch_update in modules/mailchimp_lists/mailchimp_lists.module
Sends batch mergevar data to Mailchimp.

File

./mailchimp.module, line 892
Mailchimp module.

Code

function mailchimp_batch_update_members($list_id, $batch, $double_in = FALSE) {
  try {

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mc_lists = mailchimp_get_api_object('MailchimpLists');
    if (!$mc_lists) {
      throw new MailchimpException('Cannot batch subscribe to list without Mailchimp API. Check API key has been entered.');
    }
    if (!empty($batch)) {

      // Create a new batch update operation for each member.
      foreach ($batch as $batch_data) {

        // TODO: Remove 'advanced' earlier? Needed at all?
        unset($batch_data['merge_vars']['advanced']);
        $parameters = array(
          'email_type' => $batch_data['email_type'],
          'merge_fields' => (object) $batch_data['merge_vars'],
        );
        $mc_lists
          ->addOrUpdateMember($list_id, $batch_data['email'], $parameters, TRUE);
      }

      // Process batch operations.
      return $mc_lists
        ->processBatchOperations();
    }
  } catch (Exception $e) {
    watchdog('mailchimp', 'An error occurred performing batch subscribe/update. "%message"', array(
      '%message' => $e
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }
}