Skip to content

Commit

Permalink
closes #1078 for magento 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Nov 17, 2020
1 parent a737dcc commit 325eb9f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
38 changes: 35 additions & 3 deletions Observer/Subscriber/SaveBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,63 @@ 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());

$this->_subscriberApi->update($subscriber);
}
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>

0 comments on commit 325eb9f

Please sign in to comment.