From 7f18269bf86077c0652b84da543e08f8d4a66f48 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 5 Oct 2023 09:39:16 -0300 Subject: [PATCH 01/11] #1834 for magento 2.4 --- Block/Subscribe.php | 6 +- Controller/Subscriber/Subscribe.php | 45 ++++++++++++++ Controller/WebHook/Index.php | 5 +- Cron/Webhook.php | 7 ++- Helper/Data.php | 29 ++++++--- Model/Config/Source/Maps.php | 53 +++++++++++++++++ Model/Plugin/SubscriptionManager.php | 4 ++ Observer/Subscriber/SaveBefore.php | 34 +++++++++++ etc/adminhtml/system.xml | 20 ++++++- etc/db_schema.xml | 3 + etc/db_schema_whitelist.json | 5 ++ etc/events.xml | 3 + view/adminhtml/web/js/configapikey.js | 46 ++++++++++++++- view/frontend/layout/default.xml | 5 ++ view/frontend/templates/footer.phtml | 69 ++++++++-------------- view/frontend/templates/footerwphone.phtml | 53 +++++++++++++++++ 16 files changed, 330 insertions(+), 57 deletions(-) create mode 100644 Controller/Subscriber/Subscribe.php create mode 100644 Model/Config/Source/Maps.php create mode 100644 Observer/Subscriber/SaveBefore.php create mode 100644 view/frontend/templates/footerwphone.phtml diff --git a/Block/Subscribe.php b/Block/Subscribe.php index 1012a305..7c54c05b 100644 --- a/Block/Subscribe.php +++ b/Block/Subscribe.php @@ -38,4 +38,8 @@ public function getPopupUrl() $storeId = $this->context->getStoreManager()->getStore()->getId(); return $this->helper->getConfigValue(MailchimpHelper::XML_POPUP_URL,$storeId); } -} \ No newline at end of file + public function getFormActionUrl() + { + return $this->getUrl('mailchimp/subscriber/subscribe', ['_secure' => true]); + } +} diff --git a/Controller/Subscriber/Subscribe.php b/Controller/Subscriber/Subscribe.php new file mode 100644 index 00000000..7c57a476 --- /dev/null +++ b/Controller/Subscriber/Subscribe.php @@ -0,0 +1,45 @@ +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(); + } +} diff --git a/Controller/WebHook/Index.php b/Controller/WebHook/Index.php index 9bb6131a..566a41c4 100644 --- a/Controller/WebHook/Index.php +++ b/Controller/WebHook/Index.php @@ -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; @@ -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) || @@ -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()); diff --git a/Cron/Webhook.php b/Cron/Webhook.php index 46b4afd3..0cf657d4 100644 --- a/Cron/Webhook.php +++ b/Cron/Webhook.php @@ -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); } @@ -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))); @@ -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) { diff --git a/Helper/Data.php b/Helper/Data.php index 00c9ebce..1ec6120f 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -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'; @@ -387,7 +389,7 @@ private function getAddressAtt() $ret[$item] = [ 'attCode' => $item, 'isDate' => false, - 'isAddress' => false, + 'isAddress' => true, 'options' => [] ]; } @@ -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(); } @@ -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; } @@ -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; diff --git a/Model/Config/Source/Maps.php b/Model/Config/Source/Maps.php new file mode 100644 index 00000000..3149c708 --- /dev/null +++ b/Model/Config/Source/Maps.php @@ -0,0 +1,53 @@ +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; + } +} diff --git a/Model/Plugin/SubscriptionManager.php b/Model/Plugin/SubscriptionManager.php index 21825f50..975deca4 100644 --- a/Model/Plugin/SubscriptionManager.php +++ b/Model/Plugin/SubscriptionManager.php @@ -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); diff --git a/Observer/Subscriber/SaveBefore.php b/Observer/Subscriber/SaveBefore.php new file mode 100644 index 00000000..a41ca31d --- /dev/null +++ b/Observer/Subscriber/SaveBefore.php @@ -0,0 +1,34 @@ +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; + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 2e3f5b0f..2cebc2d5 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -69,7 +69,7 @@ - + Magento\Config\Model\Config\Source\Yesno Enabling this, the "Subscribe" button at the bottom open a popup with the Mailchimp subscription form @@ -84,8 +84,24 @@ 1 + + + Magento\Config\Model\Config\Source\Yesno + + + 1 + + + + + Ebizmarts\MailChimp\Model\Config\Source\Maps + + 1 + 1 + + - + Magento\Config\Model\Config\Source\Yesno diff --git a/etc/db_schema.xml b/etc/db_schema.xml index 395f4115..87eef43a 100644 --- a/etc/db_schema.xml +++ b/etc/db_schema.xml @@ -148,4 +148,7 @@ + + +
diff --git a/etc/db_schema_whitelist.json b/etc/db_schema_whitelist.json index 1a29b7b9..26d1e21f 100644 --- a/etc/db_schema_whitelist.json +++ b/etc/db_schema_whitelist.json @@ -155,5 +155,10 @@ "mailchimp_sync_error": true, "mailchimp_sent": true } + }, + "newsletter_subscriber": { + "column": { + "phone": true + } } } diff --git a/etc/events.xml b/etc/events.xml index 0e7e46b1..1ff8bb10 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -26,6 +26,9 @@ + + + diff --git a/view/adminhtml/web/js/configapikey.js b/view/adminhtml/web/js/configapikey.js index c1870881..a7849fde 100644 --- a/view/adminhtml/web/js/configapikey.js +++ b/view/adminhtml/web/js/configapikey.js @@ -84,7 +84,21 @@ define( var abandonedCartEnabled = $('#mailchimp_abandonedcart_active').find(':selected').val(); if (ecommerceEnabled == 0 && abandonedCartEnabled == 1) { self._changeAbandonedCart(); - } + }; + $('#mailchimp_general_popup_form').change(function () { + var popupformEnabled = $('#mailchimp_general_popup_form').find(':selected').val(); + var footerphoneEnabled = $('#mailchimp_general_footer_phone').find(':selected').val(); + if (popupformEnabled == 1 && footerphoneEnabled == 1) { + self._disableFooterPhone(); + } + }); + $('#mailchimp_general_footer_phone').change(function () { + var popupformEnabled = $('#mailchimp_general_popup_form').find(':selected').val(); + var footerphoneEnabled = $('#mailchimp_general_footer_phone').find(':selected').val(); + if (popupformEnabled == 1 && footerphoneEnabled == 1) { + self._disablePopupForm(); + } + }); }, _changeEcommerce: function () { @@ -398,6 +412,36 @@ define( } }); }); + }, + _disableFooterPhone: function () { + var tag = '#mailchimp_general_footer_phone' + $(tag).empty(); + $(tag).append($('