From 661ff370e6906b981b8e9697ef44db53518be38a Mon Sep 17 00:00:00 2001 From: gonzalo Date: Mon, 9 Oct 2023 11:17:48 -0300 Subject: [PATCH 1/4] closes #1847 for magento 2.3.6-p1 --- Block/Subscribe.php | 6 +- Controller/Subscriber/Subscribe.php | 40 ++++ Controller/WebHook/Index.php | 3 +- Cron/Webhook.php | 6 +- Helper/Data.php | 25 ++- Model/Config/Source/Maps.php | 53 +++++ Model/Plugin/Subscriber.php | 211 +++++++++++++++--- Model/Plugin/SubscriptionManager.php | 8 +- Model/ResourceModel/Newsletter/Collection.php | 14 ++ Observer/Subscriber/SaveBefore.php | 34 +++ Setup/UpgradeSchema.php | 12 + etc/adminhtml/di.xml | 15 ++ etc/adminhtml/system.xml | 18 +- etc/events.xml | 3 + etc/module.xml | 2 +- .../layout/newsletter_subscriber_block.xml | 15 ++ view/adminhtml/web/js/configapikey.js | 44 ++++ view/frontend/layout/default.xml | 3 + view/frontend/templates/footerwphone.phtml | 48 ++++ 19 files changed, 521 insertions(+), 39 deletions(-) create mode 100644 Controller/Subscriber/Subscribe.php create mode 100644 Model/Config/Source/Maps.php create mode 100644 Model/ResourceModel/Newsletter/Collection.php create mode 100644 Observer/Subscriber/SaveBefore.php create mode 100644 etc/adminhtml/di.xml create mode 100644 view/adminhtml/layout/newsletter_subscriber_block.xml 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..2afde299 --- /dev/null +++ b/Controller/Subscriber/Subscribe.php @@ -0,0 +1,40 @@ +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(); + } +} diff --git a/Controller/WebHook/Index.php b/Controller/WebHook/Index.php index 9bb6131a..5aa7d193 100644 --- a/Controller/WebHook/Index.php +++ b/Controller/WebHook/Index.php @@ -52,7 +52,6 @@ public function __construct( \Ebizmarts\MailChimp\Model\MailChimpWebhookRequestFactory $chimpWebhookRequestFactory, \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress ) { - parent::__construct($context); $this->_resultFactory = $context->getResultFactory(); $this->_helper = $helper; @@ -111,6 +110,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 e4a37fd0..1a56672c 100644 --- a/Cron/Webhook.php +++ b/Cron/Webhook.php @@ -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); } @@ -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))); diff --git a/Helper/Data.php b/Helper/Data.php index 53f77ae4..98db7953 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -53,6 +53,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'; @@ -419,7 +421,7 @@ private function getAddressAtt() $ret[$item] = [ 'attCode' => $item, 'isDate' => false, - 'isAddress' => false, + 'isAddress' => true, 'options' => [] ]; } @@ -780,8 +782,14 @@ private function _getAddressValues(\Magento\Customer\Model\Address\AbstractAddre public function getMergeVarsBySubscriber(\Magento\Newsletter\Model\Subscriber $subscriber, $email = null) { $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"); + $phone = $subscriber->getPhone(); + if ($phone_field && $phone) { + $mergeVars[$phone_field] = $phone; + } + } if (!$email) { $email = $subscriber->getEmail(); } @@ -793,7 +801,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()); @@ -1031,12 +1039,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/Subscriber.php b/Model/Plugin/Subscriber.php index a3fec41b..bb60249e 100644 --- a/Model/Plugin/Subscriber.php +++ b/Model/Plugin/Subscriber.php @@ -17,27 +17,211 @@ class Subscriber * @var \Ebizmarts\MailChimp\Helper\Data */ protected $_helper; + /** + * @var \Magento\Customer\Model\Customer + */ + protected $_customer; + /** + * @var \Magento\Customer\Model\Session + */ + protected $_customerSession; /** * @var \Magento\Store\Model\StoreManagerInterface */ protected $_storeManager; - + /** + * @param \Ebizmarts\MailChimp\Helper\Data $helper + * @param \Magento\Customer\Model\ResourceModel\CustomerRepository $customer + * @param \Magento\Customer\Model\Session $customerSession + */ protected $_api = null; /** * Subscriber constructor. * @param \Ebizmarts\MailChimp\Helper\Data $helper + * @param \Magento\Customer\Model\ResourceModel\CustomerRepository $customer + * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Store\Model\StoreManagerInterface $storeManager */ public function __construct( \Ebizmarts\MailChimp\Helper\Data $helper, + \Magento\Customer\Model\ResourceModel\CustomerRepository $customer, + \Magento\Customer\Model\Session $customerSession, \Magento\Store\Model\StoreManagerInterface $storeManager ) { - + $this->_helper = $helper; + $this->_customer = $customer; + $this->_customerSession = $customerSession; $this->_storeManager = $storeManager; } + /** + * @param \Magento\Newsletter\Model\Subscriber $subscriber + * @param $customerId + * @return array + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function beforeUnsubscribeCustomerById( + \Magento\Newsletter\Model\Subscriber $subscriber, + $customerId + ) { + $storeId = $this->getStoreIdFromSubscriber($subscriber); + if ($this->_helper->isMailChimpEnabled($storeId)) { + if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, $storeId)) { + $subscriber->setImportMode(true); + } + $subscriber->loadByCustomerId($customerId); + if ($subscriber->isSubscribed()) { + $api = $this->_helper->getApi($storeId); + try { + $md5HashEmail = hash('md5', strtolower($subscriber->getSubscriberEmail())); + $api->lists->members->update( + $this->_helper->getDefaultList($storeId), + $md5HashEmail, + null, + 'unsubscribed' + ); + } catch (\Mailchimp_Error $e) { + $this->_helper->log($e->getFriendlyMessage()); + } + } + } + return [$customerId]; + } + + /** + * @param \Magento\Newsletter\Model\Subscriber $subscriber + * @param $customerId + * @return array + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + public function beforeSubscribeCustomerById( + \Magento\Newsletter\Model\Subscriber $subscriber, + $customerId + ) { + + $storeId = $this->getStoreIdFromSubscriber($subscriber); + 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(); + $mergeVars = $this->_helper->getMergeVarsBySubscriber($subscriber, $email); + $api = $this->_helper->getApi($storeId); + if ($this->_helper->isDoubleOptInEnabled($storeId)) { + $status = 'pending'; + } else { + $status = 'subscribed'; + } + 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 + ); + } catch (\Mailchimp_Error $e) { + $this->_helper->log($e->getFriendlyMessage()); + } + } + } + } + return [$customerId]; + } + + /** + * @param \Magento\Newsletter\Model\Subscriber $subscriber + * @param $email + * @return array + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + public function beforeSubscribe( + \Magento\Newsletter\Model\Subscriber $subscriber, + $email + ) { + //$websiteId = $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); + if ($this->_helper->isDoubleOptInEnabled($storeId)) { + $status = 'pending'; + } else { + $status = 'subscribed'; + } + $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 + ); + } catch (\Mailchimp_Error $e) { + $this->_helper->log($e->getFriendlyMessage()); + } + } + } + return [$email]; + } + + /** + * @param \Magento\Newsletter\Model\Subscriber $subscriber + * @return null + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function beforeUnsubscribe( + \Magento\Newsletter\Model\Subscriber $subscriber + ) { + $storeId = $this->getStoreIdFromSubscriber($subscriber); + 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); + try { + $md5HashEmail = hash('md5', strtolower($subscriber->getSubscriberEmail())); + $api->lists->members->update( + $this->_helper->getDefaultList($storeId), + $md5HashEmail, + null, + 'unsubscribed' + ); + } catch (\Mailchimp_Error $e) { + $this->_helper->log($e->getFriendlyMessage()); + } + } + } + /** * @param \Magento\Newsletter\Model\Subscriber $subscriber * @return null @@ -79,27 +263,4 @@ protected function getStoreIdFromSubscriber(\Magento\Newsletter\Model\Subscriber { return $subscriber->getStoreId(); } - - /** - * @param \Magento\Newsletter\Model\Subscriber $subscriber - * @param $email - * @param $websiteId - * @return \Magento\Newsletter\Model\Subscriber - */ - public function afterLoadBySubscriberEmail(\Magento\Newsletter\Model\Subscriber $subscriber, $email, $websiteId) - { - if ($this->_helper->isMailChimpEnabled($subscriber->getStoreId())) { - try { - if (!$this->_helper->getConfigValue( - \Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, - $subscriber->getStoreId() - )) { - $subscriber->setImportMode(true); - } - } catch (\Exception $exception) { - $this->_helper->log($exception->getMessage()); - } - } - return $subscriber; - } } diff --git a/Model/Plugin/SubscriptionManager.php b/Model/Plugin/SubscriptionManager.php index 4f75c03b..8881bf52 100644 --- a/Model/Plugin/SubscriptionManager.php +++ b/Model/Plugin/SubscriptionManager.php @@ -158,11 +158,17 @@ 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); + $phones = $subscriber->getPhone(); + $phonec = $this->_customerSession->getPhone(); + if ($this->_customerSession->getPhone()) { + $subscriber->setPhone($this->_customerSession->getPhone()); + } if ($this->_helper->isMailChimpEnabled($storeId)) { $api = $this->_helper->getApi($storeId); if ($this->_helper->isDoubleOptInEnabled($storeId)) { @@ -221,4 +227,4 @@ public function beforeUnsubscribe( } return [$email,$storeId,$confirmCode]; } - } \ No newline at end of file + } diff --git a/Model/ResourceModel/Newsletter/Collection.php b/Model/ResourceModel/Newsletter/Collection.php new file mode 100644 index 00000000..b46d2560 --- /dev/null +++ b/Model/ResourceModel/Newsletter/Collection.php @@ -0,0 +1,14 @@ +showCustomerInfo(true)->addSubscriberTypeField()->showStoreInfo(); + $this->_map['fields']['phone'] = 'main_table.phone'; + return $this; + } +} diff --git a/Observer/Subscriber/SaveBefore.php b/Observer/Subscriber/SaveBefore.php new file mode 100644 index 00000000..fe0e6a03 --- /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/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index d0c55dad..ae729720 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -753,5 +753,17 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con 'mailchimp_sent' ); } + if (version_compare($context->getVersion(), '102.3.58') < 0) { + $connection->addColumn( + $setup->getTable('newsletter_subscriber'), + 'phone', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'default' => null, + 'length' => 128, + 'comment' => 'Phone' + ] + ); + } } } diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml new file mode 100644 index 00000000..92ba3c1f --- /dev/null +++ b/etc/adminhtml/di.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 7b8b9e5b..2c73fc82 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,6 +84,22 @@ 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/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/etc/module.xml b/etc/module.xml index 10a9013b..b3761036 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -11,7 +11,7 @@ */ --> - + diff --git a/view/adminhtml/layout/newsletter_subscriber_block.xml b/view/adminhtml/layout/newsletter_subscriber_block.xml new file mode 100644 index 00000000..38b82da6 --- /dev/null +++ b/view/adminhtml/layout/newsletter_subscriber_block.xml @@ -0,0 +1,15 @@ + + + + + + + Phone + phone + phone + phone + + + + + diff --git a/view/adminhtml/web/js/configapikey.js b/view/adminhtml/web/js/configapikey.js index 69f49d51..05b72997 100644 --- a/view/adminhtml/web/js/configapikey.js +++ b/view/adminhtml/web/js/configapikey.js @@ -85,6 +85,20 @@ define( 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 () { var self = this; @@ -397,6 +411,36 @@ define( } }); }); + }, + _disableFooterPhone: function () { + var tag = '#mailchimp_general_footer_phone' + $(tag).empty(); + $(tag).append($('