13

I am running the following code (I've hidden ID's) to add/update a subscriber's interest groups in a MailChimp list:

$mailchimp->patch('lists/1234567/members/' . md5('test@test.com'), [
    'status' => 'subscribed',
    'merge_fields' => array(
        'FNAME' => 'Ben',
        'LNAME' => 'Sinclair',
    ),
    'interests' => array(
        'abcd1234' => true,
        'defg6789' => true,
    ),
]);

The interests key is what I'm having issues with.

I presumed whatever you put in this key will overwrite what currently exists.

It doesn't seem to be the case. It only adds new interests but does not remove any if the ID's are not in the array. I am not getting any errors.

Does anyone know how to overwrite interest groups? Or if that's not possible, is there a way to remove interest groups?

ekad
  • 14,436
  • 26
  • 44
  • 46
Ben Sinclair
  • 3,896
  • 7
  • 54
  • 94
  • 3
    Have you tried setting an existing interest to `false` in the PATCH? – jonrsharpe May 16 '16 at 06:30
  • @jonrsharpe Thanks again Jon. Once again you're right. Can't believe that. Their docs were a bit vague... – Ben Sinclair May 16 '16 at 06:46
  • 2
    Yeah that was just a guess! They also support a PUT for edits, maybe that has different behaviour. – jonrsharpe May 16 '16 at 06:49
  • 1
    Glad you got this sorted out. It's common to need to be able to turn on or off an interest directly without having to figure out what the current state is for all of the other interests, which is why it works this way. – TooMuchPete May 16 '16 at 18:46
  • 1
    @TooMuchPete The way it works is fine and makes sense, all they need to do is document it a bit better so I'm not pulling my hair out. – Ben Sinclair May 19 '16 at 00:03
  • What would you have needed to see? As far as I know, the whole API works like that, and it's pretty explicit in the [getting started](http://developer.mailchimp.com/documentation/mailchimp/guides/get-started-with-mailchimp-api-3/#http-methods) docs. – TooMuchPete May 19 '16 at 18:53
  • 2
    @TooMuchPete Not sure what you're reading but how it handles adding/removing interest groups is pretty vague to me. – Ben Sinclair May 27 '16 at 00:26
  • 1
    @BenSinclair I agree their documentation is vague as far as using that method to add new subscribers. Really disappointed with v3 documentation compared to the huge amount they had for v2. Especially with them trying to EOL v2 at the end of the year (they've since canceled that). – CJdriver Oct 28 '16 at 12:16

1 Answers1

23

For completion I wanted to add this answer so people stumbling upon this post can find a quick solution.

$mailchimp->patch('lists/1234567/members/' . md5('test@test.com'), [
    'status' => 'subscribed',
    'merge_fields' => array(
        'FNAME' => 'Ben',
        'LNAME' => 'Sinclair',
    ),
    'interests' => array(
        'abcd1234' => true, // Attached
        'defg6789' => false, // Detached
    )
]);

In this example the interest 'abcd1234' will be attached and the interest 'defg6789' will be detached.

Other interests that are not listed will remain on their original value.

Sam
  • 4,205
  • 2
  • 17
  • 13
  • 6
    You should convert the email address to lower case too as this is what MailChimp expects when MD5ing the email. – Ric Mar 15 '18 at 09:34