Skip to content

Commit

Permalink
Adding ability to change email address in Magento, unsubscribe old em…
Browse files Browse the repository at this point in the history
…ail in MC and subscribe new one
  • Loading branch information
WinstonN committed Oct 18, 2017
1 parent bd1f831 commit 57b07cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
20 changes: 20 additions & 0 deletions app/code/community/Ebizmarts/MailChimp/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57b07cd

Please sign in to comment.