Skip to content

Commit

Permalink
Merge pull request #520 from WinstonN/master
Browse files Browse the repository at this point in the history
Adding ability to change email address in Magento, unsubscribe old em…
  • Loading branch information
Santiagoebizmarts authored Oct 18, 2017
2 parents 31f6b4b + b518173 commit bcb7722
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 @@ -128,7 +128,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

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 bcb7722

Please sign in to comment.