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

Adding ability to change email address in Magento, unsubscribe old em… #520

Merged
merged 3 commits into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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