0

I'm using MailChimp API for adding subscriber to list. I have created three type of group. I wanted to add the user to particular group. How can I do that? Here is my code:

    $MailChimp = new \Drewm\MailChimp(MAILCHIMP_KEY);

    $result = $MailChimp->call('lists/subscribe', array(
        'id'=> MAILCHIMP_LIST,
        'email' => array('email'=>$data['email']),
        'merge_vars' => array('FNAME'=>$data['first_name'],
            'LNAME'=>$data['last_name'],
            'TITLE'=>$data['title'],
            'COMPANY'=>$data['company'],
            'PROFESSION'=>$profession,
            'ADDRESS'=>'',
            'groupings' => array(array('id' => 'id1234')),
        ),
        'double_optin'      => false,
        'update_existing'   => true,
        'replace_interests' => false,
        'send_welcome'      => false,
    ));
ARV
  • 54
  • 6
  • If you're using V3 API then this is the answer you're looking for https://stackoverflow.com/a/47159262/8733158 – Kent V Nov 07 '17 at 13:36

1 Answers1

0

Reference the API Docs for details. Adding to groups requires a groupings item to your merge_vars array, which you have. That should be an array (good so far). The array should contain associative arrays (still good). Each of those should have an id containing the ID of the grouping and a groups array with the names of the groups you want to add the user to. Like so:

{
  "merge_fields": {
    "groupings": [
      {"id": "some_grouping_id", "groups": ["group1", "group2"]}
    ]
  }
}

In v3, this is a good bit easier. There's an interests object that just takes IDs and Booleans to determine if a user is added to or removed from a group.

TooMuchPete
  • 4,583
  • 2
  • 17
  • 21