Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsubscribed contact are resubscribed in CiviCRM after edition in Mailchimp #336

Open
samuelsov opened this issue Apr 6, 2020 · 3 comments

Comments

@samuelsov
Copy link
Contributor

To reproduce:

  • Take an unsubscribed contact in Mailchimp

Screenshot_2020-04-06 - Mailchimp

  • ensure that the contact is properly unsubscribed in CiviCRM

  • edit profile or groups in Mailchimp

The contact will be resubscribed to the list.

@samuelsov
Copy link
Contributor Author

The following patch seems to fix it:

diff --git a/CRM/Mailchimp/Page/WebHook.php b/CRM/Mailchimp/Page/WebHook.php
index 9bd13d9..3eaec26 100644
--- a/CRM/Mailchimp/Page/WebHook.php
+++ b/CRM/Mailchimp/Page/WebHook.php
@@ -350,11 +366,19 @@ class CRM_Mailchimp_Page_WebHook extends CRM_Core_Page {
       civicrm_api3('Contact', 'create', ['contact_id' => $this->contact_id] + $edits);
     }
 
+    // [SV] fixing a bug where the contact is unsubscribed in Mailchimp but added here
+    // it would happen any time an admin update an account in mailchimp
+    $api = CRM_Mailchimp_Utils::getMailchimpApi();
+    $subscriber_hash = md5(strtolower($this->request_data['email']));
+    $list_id = $this->request_data['list_id'];
+    $response = $api->get("/lists/{$list_id}/members/{$subscriber_hash}", ['fields' => 'status']);
+    $status = $response->data->status == 'unsubscribed' ? 'Removed' : 'Added';
+
     // Contact has just subscribed, we'll need to add them to the list.
     civicrm_api3('GroupContact', 'create', [
       'contact_id' => $this->contact_id,
       'group_id'   => $this->sync->membership_group_id,
-      'status'     => 'Added',
+      'status'     => $status,
       ]);
 
     $this->updateInterestsFromMerges();

Not sure my code works for all cases as I assume only unsubscibed is Removed but the status value is one of:

  • subscribed
  • unsubscribed
  • cleaned
  • pending
  • transactional

@samuelsov
Copy link
Contributor Author

I'm trying to see any difference between Mailchimp and CiviCRM list and:

  • cleaned => hard bounce so we should ideally put the email on_hold in CiviCRM or have a way to identify them

@samuelsov
Copy link
Contributor Author

I'm trying to see any difference between Mailchimp and CiviCRM list and:

* cleaned => hard bounce so we should ideally put the email on_hold in CiviCRM or have a way to identify them

I've seen that it's already in the code so let's ignore the cleaned part and focus on the bug described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant