Skip to content

Commit c5ff022

Browse files
committed
closes #1847 for magento 2.3
1 parent 1b537c3 commit c5ff022

File tree

18 files changed

+339
-33
lines changed

18 files changed

+339
-33
lines changed

Block/Subscribe.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ public function getPopupUrl()
3838
$storeId = $this->context->getStoreManager()->getStore()->getId();
3939
return $this->helper->getConfigValue(MailchimpHelper::XML_POPUP_URL,$storeId);
4040
}
41-
}
41+
public function getFormActionUrl()
42+
{
43+
return $this->getUrl('mailchimp/subscriber/subscribe', ['_secure' => true]);
44+
}
45+
}

Controller/Subscriber/Subscribe.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Ebizmarts\MailChimp\Controller\Subscriber;
4+
5+
use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
6+
use Magento\Customer\Model\Session;
7+
use Magento\Customer\Model\Url as CustomerUrl;
8+
use Magento\Framework\App\Action\Context;
9+
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
use Magento\Framework\Validator\EmailAddress as EmailValidator;
11+
use Magento\Newsletter\Controller\Subscriber\NewAction as SubscriberController;
12+
use Magento\Newsletter\Model\SubscriberFactory;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
15+
class Subscribe extends SubscriberController implements HttpPostActionInterface
16+
{
17+
private $session;
18+
public function __construct(
19+
Context $context,
20+
SubscriberFactory $subscriberFactory,
21+
Session $customerSession,
22+
StoreManagerInterface $storeManager,
23+
CustomerUrl $customerUrl,
24+
CustomerAccountManagement $customerAccountManagement,
25+
EmailValidator $emailValidator = null
26+
)
27+
{
28+
$this->session = $customerSession;
29+
parent::__construct($context, $subscriberFactory, $customerSession, $storeManager, $customerUrl, $customerAccountManagement, $emailValidator);
30+
}
31+
32+
public function execute()
33+
{
34+
if($this->getRequest()->isPost() && $this->getRequest()->getPost('phone')) {
35+
$phone = (string)$this->getRequest()->getPost('phone');
36+
$this->session->setPhone($phone);
37+
}
38+
return parent::execute();
39+
}
40+
}

Controller/WebHook/Index.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function __construct(
5252
\Ebizmarts\MailChimp\Model\MailChimpWebhookRequestFactory $chimpWebhookRequestFactory,
5353
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
5454
) {
55-
5655
parent::__construct($context);
5756
$this->_resultFactory = $context->getResultFactory();
5857
$this->_helper = $helper;
@@ -111,6 +110,8 @@ public function execute()
111110
$this->_helper->log($request['data']);
112111
$result->setHttpResponseCode(403);
113112
}
113+
} else {
114+
$this->_helper->log("The two way is off");
114115
}
115116
} else {
116117
$this->_helper->log('An empty request comes from ip: '.$this->_remoteAddress->getRemoteAddress());

Cron/Webhook.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ protected function _subscribe($data)
150150
if (count($storeIds) > 0) {
151151
foreach ($storeIds as $storeId) {
152152
$sub = $this->_subscriberFactory->create();
153-
$sub->setStoreId($storeId);
153+
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
154+
$sub->setStoreId($websiteId);
154155
$sub->setSubscriberEmail($email);
155156
$this->_subscribeMember($sub, \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED);
156157
}
@@ -247,7 +248,8 @@ protected function _profile($data)
247248

248249
$stores = $this->_helper->getMagentoStoreIdsByListId($listId);
249250
if (count($stores)) {
250-
$subscriber->setStoreId($stores[0]);
251+
$websiteId = $this->storeManager->getStore($stores[0])->getWebsiteId();
252+
$subscriber->setStoreId($websiteId);
251253
try {
252254
$api = $this->_helper->getApi($stores[0]);
253255
$member = $api->lists->members->get($listId, hash('md5', strtolower($email)));

Helper/Data.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
5353
const XML_POPUP_FORM = 'mailchimp/general/popup_form';
5454
const XML_POPUP_URL = 'mailchimp/general/popup_url';
5555
const XML_CLEAN_ERROR_MONTHS = 'mailchimp/ecommerce/clean_errors_months';
56-
56+
const XML_FOOTER_PHONE = 'mailchimp/general/footer_phone';
57+
const XML_FOOTER_MAP = 'mailchimp/general/footer_phone_map';
5758
const ORDER_STATE_OK = 'complete';
5859

5960
const GUEST_GROUP = 'NOT LOGGED IN';
@@ -424,7 +425,7 @@ private function getAddressAtt()
424425
$ret[$item] = [
425426
'attCode' => $item,
426427
'isDate' => false,
427-
'isAddress' => false,
428+
'isAddress' => true,
428429
'options' => []
429430
];
430431
}
@@ -787,6 +788,14 @@ public function getMergeVarsBySubscriber(\Magento\Newsletter\Model\Subscriber $s
787788
$mergeVars = [];
788789
$storeId = $subscriber->getStoreId();
789790
$webSiteId = $this->getWebsiteId($subscriber->getStoreId());
791+
$webSiteId = $subscriber->getStoreId();
792+
if ($this->getConfigValue(self::XML_FOOTER_PHONE, $webSiteId, "websites")) {
793+
$phone_field = $this->getConfigValue(self::XML_FOOTER_MAP , $webSiteId, "websites");
794+
$phone = $subscriber->getPhone();
795+
if ($phone_field && $phone) {
796+
$mergeVars[$phone_field] = $phone;
797+
}
798+
}
790799
if (!$email) {
791800
$email = $subscriber->getEmail();
792801
}
@@ -798,7 +807,7 @@ public function getMergeVarsBySubscriber(\Magento\Newsletter\Model\Subscriber $s
798807
$customer->setWebsiteId($webSiteId);
799808
$customer->loadByEmail($email);
800809
if ($customer->getData('email') == $email) {
801-
$mergeVars = $this->getMergeVars($customer, $storeId);
810+
$mergeVars = array_merge($mergeVars,$this->getMergeVars($customer, $customer->getStoreId()));
802811
}
803812
} catch (\Exception $e) {
804813
$this->log($e->getMessage());
@@ -1035,12 +1044,15 @@ public function deleteWebHook($apikey, $listId)
10351044
public function loadListSubscribers($listId, $mail)
10361045
{
10371046
$collection = null;
1047+
$websiteIds = [];
10381048
$storeIds = $this->getMagentoStoreIdsByListId($listId);
1039-
$storeIds[] = 0;
1049+
foreach($storeIds as $storeId) {
1050+
$websiteIds[] =$this->_storeManager->getStore($storeId)->getWebsiteId();
1051+
}
10401052
if (count($storeIds) > 0) {
10411053
$collection = $this->_subscriberCollection->create();
10421054
$collection
1043-
->addFieldToFilter('store_id', ['in'=>$storeIds])
1055+
->addFieldToFilter('store_id', ['in'=>$websiteIds])
10441056
->addFieldToFilter('subscriber_email', ['eq'=>$mail]);
10451057
}
10461058
return $collection;

Model/Config/Source/Maps.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Ebizmarts\MailChimp\Model\Config\Source;
4+
5+
use Magento\Framework\Data\OptionSourceInterface;
6+
use Ebizmarts\MailChimp\Helper\Data as MailchimpHelper;
7+
use Magento\Framework\App\RequestInterface;
8+
class Maps implements OptionSourceInterface
9+
{
10+
private $options = null;
11+
public function __construct(
12+
RequestInterface $request,
13+
MailchimpHelper $helper
14+
)
15+
{
16+
$storeId = (int) $request->getParam("store", 0);
17+
if ($request->getParam('website', 0)) {
18+
$scope = 'website';
19+
$storeId = $request->getParam('website', 0);
20+
} elseif ($request->getParam('store', 0)) {
21+
$scope = 'stores';
22+
$storeId = $request->getParam('store', 0);
23+
} else {
24+
$scope = 'default';
25+
}
26+
27+
if ($helper->getApiKey($storeId, $scope)) {
28+
try {
29+
$this->options = $helper->getApi($storeId, $scope)->lists->mergeFields->getAll(
30+
$helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST, $storeId, $scope),
31+
null,
32+
null,
33+
MailchimpHelper::MAX_MERGEFIELDS
34+
);
35+
} catch (\Mailchimp_Error $e) {
36+
$helper->log($e->getFriendlyMessage());
37+
}
38+
}
39+
40+
}
41+
public function toOptionArray()
42+
{
43+
if (is_array($this->options)&&key_exists('merge_fields', $this->options)) {
44+
$rc = [];
45+
foreach ($this->options['merge_fields'] as $item) {
46+
$rc[$item['tag']] = $item['tag'] . ' (' . $item['name'] . ' : ' . $item['type'] . ')';
47+
}
48+
} else {
49+
$rc[] = ['value' => 0, 'label' => __('---No Data---')];
50+
}
51+
return $rc;
52+
}
53+
}

Model/Plugin/Subscriber.php

+12-22
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(
4949
\Magento\Customer\Model\Session $customerSession,
5050
\Magento\Store\Model\StoreManagerInterface $storeManager
5151
) {
52-
52+
5353
$this->_helper = $helper;
5454
$this->_customer = $customer;
5555
$this->_customerSession = $customerSession;
@@ -64,9 +64,9 @@ public function __construct(
6464
*/
6565
public function beforeUnsubscribeCustomerById(
6666
\Magento\Newsletter\Model\Subscriber $subscriber,
67-
$customerId
67+
$customerId
6868
) {
69-
$storeId = $this->getStoreIdFromSubscriber($subscriber);
69+
$storeId = $this->_storeManager->getStore()->getId();
7070
if ($this->_helper->isMailChimpEnabled($storeId)) {
7171
if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, $storeId)) {
7272
$subscriber->setImportMode(true);
@@ -99,10 +99,9 @@ public function beforeUnsubscribeCustomerById(
9999
*/
100100
public function beforeSubscribeCustomerById(
101101
\Magento\Newsletter\Model\Subscriber $subscriber,
102-
$customerId
102+
$customerId
103103
) {
104-
105-
$storeId = $this->getStoreIdFromSubscriber($subscriber);
104+
$storeId = $this->_storeManager->getStore()->getId();
106105
if ($this->_helper->isMailChimpEnabled($storeId)) {
107106
$subscriber->loadByCustomerId($customerId);
108107
if (!$subscriber->isSubscribed()) {
@@ -152,15 +151,16 @@ public function beforeSubscribeCustomerById(
152151
*/
153152
public function beforeSubscribe(
154153
\Magento\Newsletter\Model\Subscriber $subscriber,
155-
$email
154+
$email
156155
) {
157-
158-
$storeId = $this->getStoreIdFromSubscriber($subscriber);
156+
$storeId = $this->_storeManager->getStore()->getId();
159157
if ($this->_helper->isMailChimpEnabled($storeId)) {
160158
if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, $storeId)) {
161159
$subscriber->setImportMode(true);
162160
}
163-
$storeId = $this->_storeManager->getStore()->getId();
161+
if ($this->_customerSession->getPhone()) {
162+
$subscriber->setPhone($this->_customerSession->getPhone());
163+
}
164164

165165
if ($this->_helper->isMailChimpEnabled($storeId)) {
166166
$api = $this->_helper->getApi($storeId);
@@ -201,7 +201,7 @@ public function beforeSubscribe(
201201
public function beforeUnsubscribe(
202202
\Magento\Newsletter\Model\Subscriber $subscriber
203203
) {
204-
$storeId = $this->getStoreIdFromSubscriber($subscriber);
204+
$storeId = $this->_storeManager->getStore()->getId();
205205
if ($this->_helper->isMailChimpEnabled($storeId)) {
206206
if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, $storeId)) {
207207
$subscriber->setImportMode(true);
@@ -228,8 +228,7 @@ public function beforeUnsubscribe(
228228
public function afterDelete(
229229
\Magento\Newsletter\Model\Subscriber $subscriber
230230
) {
231-
232-
$storeId = $this->getStoreIdFromSubscriber($subscriber);
231+
$storeId = $this->_storeManager->getStore()->getId();
233232
if ($this->_helper->isMailChimpEnabled($storeId)) {
234233
$api = $this->_helper->getApi($storeId);
235234
if ($subscriber->isSubscribed()) {
@@ -252,13 +251,4 @@ public function afterDelete(
252251
}
253252
return null;
254253
}
255-
256-
/**
257-
* @param \Magento\Newsletter\Model\Subscriber $subscriber
258-
* @return int
259-
*/
260-
protected function getStoreIdFromSubscriber(\Magento\Newsletter\Model\Subscriber $subscriber)
261-
{
262-
return $subscriber->getStoreId();
263-
}
264254
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Ebizmarts\MailChimp\Model\ResourceModel\Newsletter;
4+
5+
class Collection extends \Magento\Newsletter\Model\ResourceModel\Subscriber\Collection
6+
{
7+
protected function _initSelect()
8+
{
9+
parent::_initSelect();
10+
$this->showCustomerInfo(true)->addSubscriberTypeField()->showStoreInfo();
11+
$this->_map['fields']['phone'] = 'main_table.phone';
12+
return $this;
13+
}
14+
}

Observer/Subscriber/SaveBefore.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Ebizmarts\MailChimp\Observer\Subscriber;
4+
5+
use Magento\Framework\Event\Observer;
6+
use Magento\Customer\Model\Session;
7+
8+
class SaveBefore implements \Magento\Framework\Event\ObserverInterface
9+
{
10+
/**
11+
* @var Session
12+
*/
13+
private $customerSession;
14+
15+
/**
16+
* @param Session $customerSession
17+
*/
18+
public function __construct(
19+
Session $customerSession
20+
)
21+
{
22+
$this->customerSession = $customerSession;
23+
}
24+
public function execute(Observer $observer)
25+
{
26+
// TODO: Implement execute() method.
27+
$subscriber = $observer->getSubscriber();
28+
if ($this->customerSession->getPhone()) {
29+
$subscriber->setPhone($this->customerSession->getPhone());
30+
$this->customerSession->unsPhone();
31+
}
32+
return $subscriber;
33+
}
34+
}

Setup/UpgradeSchema.php

+12
Original file line numberDiff line numberDiff line change
@@ -753,5 +753,17 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
753753
'mailchimp_sent'
754754
);
755755
}
756+
if (version_compare($context->getVersion(), '102.3.58') < 0) {
757+
$connection->addColumn(
758+
$setup->getTable('newsletter_subscriber'),
759+
'phone',
760+
[
761+
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
762+
'default' => null,
763+
'length' => 128,
764+
'comment' => 'Phone'
765+
]
766+
);
767+
}
756768
}
757769
}

etc/adminhtml/di.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Ebizmarts_MailChimp Magento JS component
5+
*
6+
* @category Ebizmarts
7+
* @package Ebizmarts_MailChimp
8+
* @author Ebizmarts Team <[email protected]>
9+
* @copyright Ebizmarts (http://ebizmarts.com)
10+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
11+
*/
12+
-->
13+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
14+
<preference for="Magento\Newsletter\Model\ResourceModel\Subscriber\Grid\Collection" type="Ebizmarts\MailChimp\Model\ResourceModel\Newsletter\Collection" />
15+
</config>

0 commit comments

Comments
 (0)