Updating a member's group interests using MailChimp API V3
Asked Answered
F

1

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('[email protected]'), [
    '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?

Fagaceous answered 16/5, 2016 at 6:17 Comment(8)
Have you tried setting an existing interest to false in the PATCH?Obsolete
@Obsolete Thanks again Jon. Once again you're right. Can't believe that. Their docs were a bit vague...Fagaceous
Yeah that was just a guess! They also support a PUT for edits, maybe that has different behaviour.Obsolete
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.Coady
@Coady 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.Fagaceous
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 docs.Coady
@Coady Not sure what you're reading but how it handles adding/removing interest groups is pretty vague to me.Fagaceous
@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).Yahairayahata
C
24

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('[email protected]'), [
    '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.

Conjure answered 10/2, 2017 at 15:19 Comment(1)
You should convert the email address to lower case too as this is what MailChimp expects when MD5ing the email.Underpants

© 2022 - 2024 — McMap. All rights reserved.