Skip to content

Commit

Permalink
#1834 for magento 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Oct 5, 2023
1 parent 0e2e6ca commit 7f18269
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 57 deletions.
6 changes: 5 additions & 1 deletion 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]);
}
}
45 changes: 45 additions & 0 deletions Controller/Subscriber/Subscribe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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')) {
$email = (string)$this->getRequest()->getPost('email');
$phone = (string)$this->getRequest()->getPost('phone');
$this->session->setPhone($phone);
}
return parent::execute();
}
}
5 changes: 4 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 @@ -94,6 +94,7 @@ public function execute()
$result->setHttpResponseCode(403);
return $result;
}
$this->_helper->log($this->getRequest()->getPost());
if ($this->getRequest()->getPost('type')) {
$request = $this->getRequest()->getPost();
if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_WEBHOOK_ACTIVE) ||
Expand All @@ -111,6 +112,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
7 changes: 5 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 Expand Up @@ -307,6 +309,7 @@ protected function _processMerges(\Magento\Customer\Model\Customer $customer, $d
}
} else {
if (is_array($mapFields)) {
$this->_helper->log($mapFields);
foreach ($mapFields as $map) {
if ($map['mailchimp'] == $key) {
if (!$map['isAddress'] && $map['customer_field'] != "dob" && strpos($map['customer_field'], '##') !== false) {
Expand Down
29 changes: 22 additions & 7 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +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 @@ -387,7 +389,7 @@ private function getAddressAtt()
$ret[$item] = [
'attCode' => $item,
'isDate' => false,
'isAddress' => false,
'isAddress' => true,
'options' => []
];
}
Expand Down Expand Up @@ -719,9 +721,18 @@ private function _getAddressValues(\Magento\Customer\Model\Address\AbstractAddre

public function getMergeVarsBySubscriber(\Magento\Newsletter\Model\Subscriber $subscriber, $email = null)
{
$this->log(__METHOD__);
$mergeVars = [];
$storeId = $subscriber->getStoreId();
$webSiteId = $this->getWebsiteId($subscriber->getStoreId());
$webSiteId = $subscriber->getStoreId();
if ($this->getConfigValue(self::XML_FOOTER_PHONE, $webSiteId, "websites")) {
$phone_field = $this->getConfigValue(self::XML_FOOTER_MAP , $webSiteId, "websites");
$this->log("Phone field [$phone_field]");
$phone = $subscriber->getPhone();
$this->log("Phone [$phone]");
if ($phone_field && $subscriber->getPhone()) {
$mergeVars[$phone_field] = $subscriber->getPhone();
}
}
if (!$email) {
$email = $subscriber->getEmail();
}
Expand All @@ -733,11 +744,12 @@ 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());
}
$this->log($mergeVars);
return $mergeVars;
}

Expand Down Expand Up @@ -971,12 +983,15 @@ public function deleteWebHook($apikey, $listId)
public function loadListSubscribers($listId, $mail)
{
$collection = null;
$websiteIds = [];
$storeIds = $this->getMagentoStoreIdsByListId($listId);
$storeIds[] = 0;
if (count($storeIds) > 0) {
foreach($storeIds as $storeId) {
$websiteIds[] =$this->_storeManager->getStore($storeId)->getWebsiteId();
}
if (count($websiteIds) > 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;
}
}
4 changes: 4 additions & 0 deletions Model/Plugin/SubscriptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,14 @@ public function beforeSubscribe(
$email,
$storeId
) {
$this->_helper->log(__METHOD__);
if ($this->_helper->isMailChimpEnabled($storeId)) {
$websiteId = (int)$this->_storeManager->getStore($storeId)->getWebsiteId();

$subscriber = $this->_subscriberFactory->create()->loadBySubscriberEmail($email, $websiteId);
if ($this->_customerSession->getPhone()) {
$subscriber->setPhone($this->_customerSession->getPhone());
}

if ($this->_helper->isMailChimpEnabled($storeId)) {
$api = $this->_helper->getApi($storeId);
Expand Down
34 changes: 34 additions & 0 deletions Observer/Subscriber/SaveBefore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Ebizmarts\MailChimp\Observer\Subscriber;

use Magento\Framework\Event\Observer;
use Magento\Customer\Model\Session;

class SaveBefore implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var Session
*/
private $customerSession;

/**
* @param Session $customerSession
*/
public function __construct(
Session $customerSession
)
{
$this->customerSession = $customerSession;
}
public function execute(Observer $observer)
{
// TODO: Implement execute() method.
$subscriber = $observer->getSubscriber();
if ($this->customerSession->getPhone()) {
$subscriber->setPhone($this->customerSession->getPhone());
$this->customerSession->unsPhone();
}
return $subscriber;
}
}
20 changes: 18 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
</depends>
</field>
<field id="popup_form" translate="label" type="select" sortOrder="42" showInStore="1" showInWebsite="1" showInDefault="1">
<label>Change the footer link</label>
<label>Popup footer</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enabling this, the "Subscribe" button at the bottom open a popup with the Mailchimp subscription form</comment>
<depends>
Expand All @@ -84,8 +84,24 @@
<field id="*/*/popup_form">1</field>
</depends>
</field>
<field id="footer_phone" translate="label" type="select" sortOrder="44" showInStore="1" showInWebsite="1" showInDefault="1">
<label>Add the phone in the footer</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment></comment>
<depends>
<field id="*/*/active">1</field>
</depends>
</field>
<field id="footer_phone_map" translate="label" type="select" sortOrder="45" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Mailchimp map for phone</label>
<source_model>Ebizmarts\MailChimp\Model\Config\Source\Maps</source_model>
<depends>
<field id="*/*/active">1</field>
<field id="*/*/footer_phone">1</field>
</depends>
</field>

<field id="magentoemail" translate="label" type="select" sortOrder="45" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="magentoemail" translate="label" type="select" sortOrder="48" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Magento always manage emails</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
Expand Down
3 changes: 3 additions & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,7 @@
<column name="id"/>
</constraint>
</table>
<table name="newsletter_subscriber">
<column xsi:type="varchar" name="phone" length="50" comment="Phone of the subscriber"/>
</table>
</schema>
5 changes: 5 additions & 0 deletions etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,10 @@
"mailchimp_sync_error": true,
"mailchimp_sent": true
}
},
"newsletter_subscriber": {
"column": {
"phone": true
}
}
}
3 changes: 3 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<event name="newsletter_subscriber_save_after">
<observer name="mailchimp_newsletter_subscriber_save_after" instance="\Ebizmarts\MailChimp\Observer\Subscriber\SaveAfter" />
</event>
<event name="newsletter_subscriber_save_before">
<observer name="mailchimp_newsletter_subscriber_save_before" instance="\Ebizmarts\MailChimp\Observer\Subscriber\SaveBefore" />
</event>
<event name="catalog_product_import_bunch_save_after">
<observer name="mailchimp_product_import" instance="\Ebizmarts\MailChimp\Observer\Adminhtml\Product\ImportAfter"/>
</event>
Expand Down
Loading

0 comments on commit 7f18269

Please sign in to comment.