Is it possible to use mailchimp_subscribe() to remove interest groups from a user?

I tried passing in an array of interest groups to mailchimp_subscribe() as outlined in https://www.drupal.org/node/2767865. Everything goes great for subscribing a member to a new interest group, but there's a problem if you want to remove an interest group that a current subscriber is in.

I've tried 3 methods.

First, I passed an array structured something like this for the $interests parameter to mailchimp_subscribe():

"interests": {
        "123123123": {
            "914f2a603e": true,
            "4d0221bbff": false,
            "2a791dc6c8": true
         }
}

When I print out the results of the variable $selected_interests in mailchimp.module, I get back the following:

"interests": {
        "123123123": {
            "914f2a603e": true,
            "4d0221bbff": true,
            "2a791dc6c8": true
         }
}

On lines 463-472 of mailchimp.module, it takes every value you get and sets it to TRUE, regardless of the value you sent in the first place. This code is used again on line 635-642.

// Set interests.
    if (!empty($interests)) {
      $selected_interests = array();
      foreach ($interests as $interest_group) {
        foreach ($interest_group as $interest_id => $interest_status) {
          $selected_interests[$interest_id] = ($interest_status !== 0);
        }
      }

Second, I've also tried to send only the interests that I want to keep when calling mailchimp_subscribe(), but that does not seem to remove the interest group that is missing when making the mailchimp API call.

Lastly, I edited out the requirement in the above code, replacing

 $selected_interests[$interest_id] = ($interest_status !== 0);

with

 $selected_interests[$interest_id] = $interest_status;

This passes onto the $mc_lists->addOrUpdateMember a $parameters argument that has this for the interests:

"interests": {
        "123123123": {
            "914f2a603e": true,
            "4d0221bbff": false,
            "2a791dc6c8": true
         }
}

When I run this, my interest group is removed successfully in Mailchimp.

Unless I'm missing something, there is no easy way to programmatically remove an interest group from a subscriber in a list.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tomdisher created an issue. See original summary.

tomdisher’s picture

Title: How can one programmatically remove interest groups using mailchimp_update_member()? » Programmatically removing interest groups using mailchimp_subscribe()
tomdisher’s picture

This patch might be a simple way to handle it. If you do not specify a value for the interest group key, it will default to TRUE. However, if you pass a interest group key with a valueFALSE, mailchimp_subscribe will be able to remove the interest group from the user you specify.

tomdisher’s picture

Version: 7.x-4.4 » 7.x-4.x-dev
samuel.mortenson’s picture

Status: Active » Closed (won't fix)

Hey @tomdisher, I see the use case for this but we're in maintenance-mode for the Drupal 7 releases of Mailchimp, so only bug fixes can be committed going forward. Any new features should be developed for the Drupal 8 releases of Mailchimp. Thanks!