You are here

function mailchimp_segment_batch_add_subscribers in Mailchimp 7.5

Same name and namespace in other branches
  1. 7.3 mailchimp.module \mailchimp_segment_batch_add_subscribers()
  2. 7.4 mailchimp.module \mailchimp_segment_batch_add_subscribers()

Add a batch of email addresses to a static segment of a list.

Parameters

string $list_id: ID of a MailChimp list.

string $segment_id: ID of a segment of the MailChimp list.

array $batch: Batch of email addresses to add to the segment (does NOT subscribe new).

Return value

int Successful subscribe count

3 calls to mailchimp_segment_batch_add_subscribers()
MailchimpListsTestCase::testAddSegmentBatchSubscribers in modules/mailchimp_lists/tests/mailchimp_lists.test
Tests adding batch subscribers to a list segment.
mailchimp_lists_add_to_segment_action in modules/mailchimp_lists/mailchimp_lists.module
Action function for the Add To Segment action.
mailchimp_segment_add_subscriber in ./mailchimp.module
Add a specific subscriber to a static segment of a list.
1 string reference to 'mailchimp_segment_batch_add_subscribers'
mailchimp_segment_add_subscriber in ./mailchimp.module
Add a specific subscriber to a static segment of a list.

File

./mailchimp.module, line 1337
Mailchimp module.

Code

function mailchimp_segment_batch_add_subscribers($list_id, $segment_id, $batch) {
  $count = 0;
  try {

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mc_lists = mailchimp_get_api_object('MailchimpLists');
    if (!$mc_lists) {
      throw new MailchimpException('Cannot batch add segment subscribers without MailChimp API. Check API key has been entered.');
    }
    $segments_data = $mc_lists
      ->getSegments($list_id, array(
      'count' => 500,
    ));
    $matched_segment = NULL;
    foreach ($segments_data->segments as $segment) {
      if ($segment->id == $segment_id) {
        $matched_segment = $segment;
        continue;
      }
    }
    if ($matched_segment != NULL) {
      $parameters = array(
        'static_segment' => $batch,
      );
      $result = $mc_lists
        ->updateSegment($list_id, $segment_id, $matched_segment->name, $parameters);
      $count = isset($result->member_count) ? $result->member_count : 0;
    }
  } catch (Exception $e) {
    watchdog('mailchimp', 'An error occurred on batch segment add. Audience: @list_id Segment: @segment_id. "%message"', array(
      '@list_id' => $list_id,
      '@segment_id' => $segment_id,
      '%message' => $e
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }
  return $count;
}