From 57b07cda38a2070667d2d7483b5f841f7fa7dd0c Mon Sep 17 00:00:00 2001 From: Winston Nolan Date: Wed, 18 Oct 2017 16:30:39 +0200 Subject: [PATCH 1/2] Adding ability to change email address in Magento, unsubscribe old email in MC and subscribe new one --- .../MailChimp/Model/Api/Subscribers.php | 2 +- .../Ebizmarts/MailChimp/Model/Observer.php | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php index ee2a12c17..969fbe2f6 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php @@ -124,7 +124,7 @@ public function getMergeVars($subscriber) ->getData(); $mergeVars = array(); $subscriberEmail = $subscriber->getSubscriberEmail(); - $customer = Mage::getModel('customer/customer')->setWebsiteId($websiteId)->loadByEmail($subscriberEmail); + $customer = Mage::getModel('customer/customer')->setWebsiteId($websiteId)->load($subscriber->getCustomerId()); foreach ($maps as $map) { $customAtt = $map['magento']; diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index e48759063..241e6bbde 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -127,6 +127,26 @@ public function customerSaveBefore(Varien_Event_Observer $observer) $customer = $observer->getEvent()->getCustomer(); $storeId = $customer->getStoreId(); + if($customer->getOrigData('email')) { + // check if customer has changed email address + if($customer->getOrigData('email') != $customer->getEmail()) { + + // unsubscribe old email address + $subscriber = Mage::getModel('newsletter/subscriber'); + $subscriber + ->setSubscriberEmail($customer->getOrigData('email')) + ->setStoreId($storeId); + + Mage::getModel('mailchimp/api_subscribers')->deleteSubscriber($subscriber); + + // subscribe new email address + $subscriber = Mage::getModel('newsletter/subscriber')->loadByCustomer($customer); + $subscriber->setSubscriberEmail($customer->getEmail()); // make sure we set the new email address + + $response = Mage::getModel('mailchimp/api_subscribers')->updateSubscriber($subscriber, true); + } + } + //update mailchimp ecommerce data for that customer Mage::getModel('mailchimp/api_customers')->update($customer->getId(), $storeId); //update subscriber data if a subscriber with the same email address exists From bdef5d066300f14a640e83f409cdd5054264095d Mon Sep 17 00:00:00 2001 From: Winston Nolan Date: Wed, 18 Oct 2017 17:13:40 +0200 Subject: [PATCH 2/2] Keep same code structure --- app/code/community/Ebizmarts/MailChimp/Model/Observer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 241e6bbde..a47c9a1a7 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -143,7 +143,7 @@ public function customerSaveBefore(Varien_Event_Observer $observer) $subscriber = Mage::getModel('newsletter/subscriber')->loadByCustomer($customer); $subscriber->setSubscriberEmail($customer->getEmail()); // make sure we set the new email address - $response = Mage::getModel('mailchimp/api_subscribers')->updateSubscriber($subscriber, true); + Mage::getModel('mailchimp/api_subscribers')->updateSubscriber($subscriber, true); } }