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

closes #1078 for magento 2.1 #1082

Merged
merged 1 commit into from
Nov 17, 2020
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
35 changes: 34 additions & 1 deletion Observer/Subscriber/SaveBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,64 @@ class SaveBefore implements \Magento\Framework\Event\ObserverInterface
* @var \Ebizmarts\MailChimp\Model\Api\Subscriber
*/
protected $_subscriberApi;
/**
* @var \Magento\Newsletter\Model\SubscriberFactory
*/
protected $_subscriberFactory;

/**
* SaveBefore constructor.
* @param \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $ecommerce
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Ebizmarts\MailChimp\Model\Api\Subscriber $subscriberApi
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
*/
public function __construct(
\Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $ecommerce,
\Ebizmarts\MailChimp\Helper\Data $helper,
\Ebizmarts\MailChimp\Model\Api\Subscriber $subscriberApi
\Ebizmarts\MailChimp\Model\Api\Subscriber $subscriberApi,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
) {

$this->_ecommerce = $ecommerce;
$this->_helper = $helper;
$this->_subscriberApi = $subscriberApi;
$this->_subscriberFactory = $subscriberFactory;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
/**
* @var $subscriber \Magento\Newsletter\Model\Subscriber
*/
$factory = $this->_subscriberFactory->create();
$subscriber = $observer->getSubscriber();
$subscriberOld = $factory->loadByCustomerId($subscriber->getCustomerId());

if ($this->_helper->isMailChimpEnabled($subscriberOld->getStoreId())&&$subscriber->getEmail()!=$subscriberOld->getEmail()) {
$api = $this->_helper->getApi($subscriberOld->getStoreId());
$mergeVars = $this->_helper->getMergeVarsBySubscriber($subscriberOld, $subscriberOld->getEmail());
$status = 'unsubscribed';
try {
$md5HashEmail = hash('md5', strtolower($subscriberOld->getEmail()));
$return = $api->lists->members->addOrUpdate(
$this->_helper->getDefaultList($subscriberOld->getStoreId()),
$md5HashEmail,
null,
$status,
$mergeVars,
null,
null,
null,
null,
$subscriberOld->getEmail(),
$status
);
} catch (\Mailchimp_Error $e) {
$this->_helper->log($e->getFriendlyMessage());
}

}
$this->_subscriberApi->update($subscriber);
}
}
3 changes: 3 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@
<event name="catalog_product_save_after">
<observer name="mailchimp_product_save_after" instance="\Ebizmarts\MailChimp\Observer\Adminhtml\Product\SaveAfter" />
</event>
<event name="newsletter_subscriber_save_before">
<observer name="'mailchimp_newsletter_subscriber_save_before" instance="\Ebizmarts\MailChimp\Observer\Subscriber\SaveBefore" />
</event>
</config>