Skip to content

Commit

Permalink
closes #1847 for magento 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Oct 10, 2023
1 parent f372c60 commit c3cd467
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 37 deletions.
4 changes: 4 additions & 0 deletions Block/Subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public function getPopupUrl()
$storeId = $this->context->getStoreManager()->getStore()->getId();
return $this->helper->getConfigValue(MailchimpHelper::XML_POPUP_URL,$storeId);
}
public function getFormActionUrl()
{
return $this->getUrl('mailchimp/subscriber/subscribe', ['_secure' => true]);
}
}
39 changes: 39 additions & 0 deletions Controller/Subscriber/Subscribe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Ebizmarts\MailChimp\Controller\Subscriber;

use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Validator\EmailAddress as EmailValidator;
use Magento\Newsletter\Controller\Subscriber\NewAction as SubscriberController;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Store\Model\StoreManagerInterface;

class Subscribe extends SubscriberController
{
private $session;
public function __construct(
Context $context,
SubscriberFactory $subscriberFactory,
Session $customerSession,
StoreManagerInterface $storeManager,
CustomerUrl $customerUrl,
CustomerAccountManagement $customerAccountManagement,
EmailValidator $emailValidator = null
)
{
$this->session = $customerSession;
parent::__construct($context, $subscriberFactory, $customerSession, $storeManager, $customerUrl, $customerAccountManagement, $emailValidator);
}

public function execute()
{
if($this->getRequest()->isPost() && $this->getRequest()->getPost('phone')) {
$phone = (string)$this->getRequest()->getPost('phone');
$this->session->setPhone($phone);
}
return parent::execute();
}
}
2 changes: 2 additions & 0 deletions Controller/WebHook/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public function execute()
$this->_helper->log($e->getMessage());
$this->_helper->log($request['data']);
}
} else {
$this->_helper->log("The two way is off");
}
} else {
$this->_helper->log('An empty request comes from ip: '.$this->_remoteAddress->getRemoteAddress());
Expand Down
6 changes: 4 additions & 2 deletions Cron/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ protected function _subscribe($data)
if (count($storeIds) > 0) {
foreach ($storeIds as $storeId) {
$sub = $this->_subscriberFactory->create();
$sub->setStoreId($storeId);
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
$sub->setStoreId($websiteId);
$sub->setSubscriberEmail($email);
$this->_subscribeMember($sub, \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED);
}
Expand Down Expand Up @@ -247,7 +248,8 @@ protected function _profile($data)

$stores = $this->_helper->getMagentoStoreIdsByListId($listId);
if (count($stores)) {
$subscriber->setStoreId($stores[0]);
$websiteId = $this->storeManager->getStore($stores[0])->getWebsiteId();
$subscriber->setStoreId($websiteId);
try {
$api = $this->_helper->getApi($stores[0]);
$member = $api->lists->members->get($listId, hash('md5', strtolower($email)));
Expand Down
21 changes: 16 additions & 5 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const XML_POPUP_FORM = 'mailchimp/general/popup_form';
const XML_POPUP_URL = 'mailchimp/general/popup_url';
const XML_CLEAN_ERROR_MONTHS = 'mailchimp/ecommerce/clean_errors_months';

const XML_FOOTER_PHONE = 'mailchimp/general/footer_phone';
const XML_FOOTER_MAP = 'mailchimp/general/footer_phone_map';

const ORDER_STATE_OK = 'complete';

Expand Down Expand Up @@ -416,7 +417,7 @@ private function getAddressAtt()
$ret[$item] = [
'attCode' => $item,
'isDate' => false,
'isAddress' => false,
'isAddress' => true,
'options' => []
];
}
Expand Down Expand Up @@ -776,6 +777,13 @@ public function getMergeVarsBySubscriber(\Magento\Newsletter\Model\Subscriber $s
$mergeVars = [];
$storeId = $subscriber->getStoreId();
$webSiteId = $this->getWebsiteId($subscriber->getStoreId());
if ($this->getConfigValue(self::XML_FOOTER_PHONE, $webSiteId, "websites")) {
$phone_field = $this->getConfigValue(self::XML_FOOTER_MAP , $webSiteId, "websites");
$phone = $subscriber->getPhone();
if ($phone_field && $phone) {
$mergeVars[$phone_field] = $phone;
}
}
if (!$email) {
$email = $subscriber->getEmail();
}
Expand All @@ -787,7 +795,7 @@ public function getMergeVarsBySubscriber(\Magento\Newsletter\Model\Subscriber $s
$customer->setWebsiteId($webSiteId);
$customer->loadByEmail($email);
if ($customer->getData('email') == $email) {
$mergeVars = $this->getMergeVars($customer, $storeId);
$mergeVars = array_merge($mergeVars,$this->getMergeVars($customer, $customer->getStoreId()));
}
} catch (\Exception $e) {
$this->log($e->getMessage());
Expand Down Expand Up @@ -1026,11 +1034,14 @@ public function loadListSubscribers($listId, $mail)
{
$collection = null;
$storeIds = $this->getMagentoStoreIdsByListId($listId);
$storeIds[] = 0;
$websiteIds = [];
foreach($storeIds as $storeId) {
$websiteIds[] =$this->_storeManager->getStore($storeId)->getWebsiteId();
}
if (count($storeIds) > 0) {
$collection = $this->_subscriberCollection->create();
$collection
->addFieldToFilter('store_id', ['in'=>$storeIds])
->addFieldToFilter('store_id', ['in'=>$websiteIds])
->addFieldToFilter('subscriber_email', ['eq'=>$mail]);
}
return $collection;
Expand Down
53 changes: 53 additions & 0 deletions Model/Config/Source/Maps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Ebizmarts\MailChimp\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;
use Ebizmarts\MailChimp\Helper\Data as MailchimpHelper;
use Magento\Framework\App\RequestInterface;
class Maps implements OptionSourceInterface
{
private $options = null;
public function __construct(
RequestInterface $request,
MailchimpHelper $helper
)
{
$storeId = (int) $request->getParam("store", 0);
if ($request->getParam('website', 0)) {
$scope = 'website';
$storeId = $request->getParam('website', 0);
} elseif ($request->getParam('store', 0)) {
$scope = 'stores';
$storeId = $request->getParam('store', 0);
} else {
$scope = 'default';
}

if ($helper->getApiKey($storeId, $scope)) {
try {
$this->options = $helper->getApi($storeId, $scope)->lists->mergeFields->getAll(
$helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST, $storeId, $scope),
null,
null,
MailchimpHelper::MAX_MERGEFIELDS
);
} catch (\Mailchimp_Error $e) {
$helper->log($e->getFriendlyMessage());
}
}

}
public function toOptionArray()
{
if (is_array($this->options)&&key_exists('merge_fields', $this->options)) {
$rc = [];
foreach ($this->options['merge_fields'] as $item) {
$rc[$item['tag']] = $item['tag'] . ' (' . $item['name'] . ' : ' . $item['type'] . ')';
}
} else {
$rc[] = ['value' => 0, 'label' => __('---No Data---')];
}
return $rc;
}
}
85 changes: 58 additions & 27 deletions Model/Plugin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {

$this->_helper = $helper;
$this->_customer = $customer;
$this->_customerSession = $customerSession;
Expand All @@ -64,9 +64,9 @@ public function __construct(
*/
public function beforeUnsubscribeCustomerById(
\Magento\Newsletter\Model\Subscriber $subscriber,
$customerId
$customerId
) {
$storeId = $this->getStoreIdFromSubscriber($subscriber);
$storeId = $this->_storeManager->getStore()->getId();
if ($this->_helper->isMailChimpEnabled($storeId)) {
if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, $storeId)) {
$subscriber->setImportMode(true);
Expand All @@ -76,7 +76,12 @@ public function beforeUnsubscribeCustomerById(
$api = $this->_helper->getApi($storeId);
try {
$md5HashEmail = hash('md5', strtolower($subscriber->getSubscriberEmail()));
$api->lists->members->update($this->_helper->getDefaultList($storeId), $md5HashEmail, null, 'unsubscribed');
$api->lists->members->update(
$this->_helper->getDefaultList($storeId),
$md5HashEmail,
null,
'unsubscribed'
);
} catch (\Mailchimp_Error $e) {
$this->_helper->log($e->getFriendlyMessage());
}
Expand All @@ -94,16 +99,15 @@ public function beforeUnsubscribeCustomerById(
*/
public function beforeSubscribeCustomerById(
\Magento\Newsletter\Model\Subscriber $subscriber,
$customerId
$customerId
) {
$storeId = $this->getStoreIdFromSubscriber($subscriber);
$storeId = $this->_storeManager->getStore()->getId();
if ($this->_helper->isMailChimpEnabled($storeId)) {
$subscriber->loadByCustomerId($customerId);
if (!$subscriber->isSubscribed()) {
if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, $storeId)) {
$subscriber->setImportMode(true);
}

if ($this->_helper->isMailChimpEnabled($storeId)) {
$customer = $this->_customer->getById($customerId);
$email = $customer->getEmail();
Expand All @@ -116,7 +120,19 @@ public function beforeSubscribeCustomerById(
}
try {
$emailHash = hash('md5', strtolower($customer->getEmail()));
$api->lists->members->addOrUpdate($this->_helper->getDefaultList($storeId), $emailHash, null, $status, $mergeVars, null, null, null, null, $email, $status);
$api->lists->members->addOrUpdate(
$this->_helper->getDefaultList($storeId),
$emailHash,
null,
$status,
$mergeVars,
null,
null,
null,
null,
$email,
$status
);
} catch (\Mailchimp_Error $e) {
$this->_helper->log($e->getFriendlyMessage());
}
Expand All @@ -131,16 +147,20 @@ public function beforeSubscribeCustomerById(
* @param $email
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function beforeSubscribe(
\Magento\Newsletter\Model\Subscriber $subscriber,
$email
$email
) {
$storeId = $this->getStoreIdFromSubscriber($subscriber);
$storeId = $this->_storeManager->getStore()->getId();
if ($this->_helper->isMailChimpEnabled($storeId)) {
if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, $storeId)) {
$subscriber->setImportMode(true);
}
if ($this->_customerSession->getPhone()) {
$subscriber->setPhone($this->_customerSession->getPhone());
}

if ($this->_helper->isMailChimpEnabled($storeId)) {
$api = $this->_helper->getApi($storeId);
Expand All @@ -152,7 +172,19 @@ public function beforeSubscribe(
$mergeVars = $this->_helper->getMergeVarsBySubscriber($subscriber, $email);
try {
$md5HashEmail = hash('md5', strtolower($email));
$return = $api->lists->members->addOrUpdate($this->_helper->getDefaultList($storeId), $md5HashEmail, null, $status, $mergeVars, null, null, null, null, $email, $status);
$return = $api->lists->members->addOrUpdate(
$this->_helper->getDefaultList($storeId),
$md5HashEmail,
null,
$status,
$mergeVars,
null,
null,
null,
null,
$email,
$status
);
} catch (\Mailchimp_Error $e) {
$this->_helper->log($e->getFriendlyMessage());
}
Expand All @@ -169,20 +201,23 @@ public function beforeSubscribe(
public function beforeUnsubscribe(
\Magento\Newsletter\Model\Subscriber $subscriber
) {
$storeId = $this->getStoreIdFromSubscriber($subscriber);
$storeId = $this->_storeManager->getStore()->getId();
if ($this->_helper->isMailChimpEnabled($storeId)) {
if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, $storeId)) {
$subscriber->setImportMode(true);
}
$api = $this->_helper->getApi($storeId);
} $api = $this->_helper->getApi($storeId);
try {
$md5HashEmail = hash('md5', strtolower($subscriber->getSubscriberEmail()));
$api->lists->members->update($this->_helper->getDefaultList($storeId), $md5HashEmail, null, 'unsubscribed');
$api->lists->members->update(
$this->_helper->getDefaultList($storeId),
$md5HashEmail,
null,
'unsubscribed'
);
} catch (\Mailchimp_Error $e) {
$this->_helper->log($e->getFriendlyMessage());
}
}
return null;
}

/**
Expand All @@ -193,14 +228,19 @@ public function beforeUnsubscribe(
public function afterDelete(
\Magento\Newsletter\Model\Subscriber $subscriber
) {
$storeId = $this->getStoreIdFromSubscriber($subscriber);
$storeId = $this->_storeManager->getStore()->getId();
if ($this->_helper->isMailChimpEnabled($storeId)) {
$api = $this->_helper->getApi($storeId);
if ($subscriber->isSubscribed()) {
try {
$md5HashEmail = hash('md5', strtolower($subscriber->getSubscriberEmail()));
if ($subscriber->getCustomerId()) {
$api->lists->members->update($this->_helper->getDefaultList($storeId), $md5HashEmail, null, 'unsubscribed');
$api->lists->members->update(
$this->_helper->getDefaultList($storeId),
$md5HashEmail,
null,
'unsubscribed'
);
} else {
$api->lists->members->delete($this->_helper->getDefaultList($storeId), $md5HashEmail);
}
Expand All @@ -211,13 +251,4 @@ public function afterDelete(
}
return null;
}

/**
* @param \Magento\Newsletter\Model\Subscriber $subscriber
* @return int
*/
protected function getStoreIdFromSubscriber(\Magento\Newsletter\Model\Subscriber $subscriber)
{
return $subscriber->getStoreId();
}
}
14 changes: 14 additions & 0 deletions Model/ResourceModel/Newsletter/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Ebizmarts\MailChimp\Model\ResourceModel\Newsletter;

class Collection extends \Magento\Newsletter\Model\ResourceModel\Subscriber\Collection
{
protected function _initSelect()
{
parent::_initSelect();
$this->showCustomerInfo(true)->addSubscriberTypeField()->showStoreInfo();
$this->_map['fields']['phone'] = 'main_table.phone';
return $this;
}
}
Loading

0 comments on commit c3cd467

Please sign in to comment.