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