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 #1847 #1848

Open
wants to merge 11 commits into
base: develop-2.4
Choose a base branch
from
43 changes: 41 additions & 2 deletions Block/Subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Ebizmarts\MailChimp\Block;

use Magento\Framework\View\Element\Template;
use \Ebizmarts\MailChimp\Helper\Data as MailchimpHelper;
use Magento\Customer\Model\Session;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\CustomerFactory;
use Ebizmarts\MailChimp\Helper\Data as MailchimpHelper;

class Subscribe extends \Magento\Newsletter\Block\Subscribe
{
Expand All @@ -15,21 +18,38 @@ class Subscribe extends \Magento\Newsletter\Block\Subscribe
* @var MailchimpHelper
*/
protected $helper;
/**
* @var Session
*/
private $customerSession;
/**
* @var CustomerRepositoryInterface
*/
private $customerRepo;
private $customerFactory;

/**
* @param Template\Context $context
* @param MailchimpHelper $helper
* @param Session $customerSession
* @param CustomerRepositoryInterface $customerRepo
* @param array $data
*/
public function __construct(
Template\Context $context,
MailchimpHelper $helper,
Session $customerSession,
CustomerRepositoryInterface $customerRepo,
CustomerFactory $customerFactory,
array $data = []
)
{
parent::__construct($context, $data);
$this->context = $context;
$this->helper = $helper;
$this->customerSession = $customerSession;
$this->customerRepo = $customerRepo;
$this->customerFactory = $customerFactory;
}

public function getPopupUrl()
Expand All @@ -38,4 +58,23 @@ 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]);
}
public function showMobilePhone()
{
$ret = true;
if ($this->customerSession->getCustomerId()) {
/**
* @var $customer \Magento\Customer\Model\Customer
*/
$customer = $this->customerFactory->create()->load($this->customerSession->getCustomerId());
$mobilePhone = $customer->getData('mobile_phone');
if ($mobilePhone&&$mobilePhone!='') {
$ret = false;
}
}
return $ret;
}
}
44 changes: 44 additions & 0 deletions Controller/Subscriber/Subscribe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Ebizmarts\MailChimp\Controller\Subscriber;

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

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

public function execute()
{
if($this->getRequest()->isPost() && $this->getRequest()->getPost('phone')) {
$phone = (string)$this->getRequest()->getPost('phone');
$this->session->setPhone($phone);
}
return parent::execute();
}
}
4 changes: 3 additions & 1 deletion Controller/WebHook/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(
\Ebizmarts\MailChimp\Model\MailChimpWebhookRequestFactory $chimpWebhookRequestFactory,
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
) {

parent::__construct($context);
$this->_resultFactory = $context->getResultFactory();
$this->_helper = $helper;
Expand Down Expand Up @@ -111,6 +111,8 @@ public function execute()
$this->_helper->log($request['data']);
$result->setHttpResponseCode(403);
}
} 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 @@ -149,8 +149,9 @@ protected function _subscribe($data)
$storeIds = $this->_helper->getMagentoStoreIdsByListId($listId);
if (count($storeIds) > 0) {
foreach ($storeIds as $storeId) {
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
$sub = $this->_subscriberFactory->create();
$sub->setStoreId($storeId);
$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
Loading