From 208ad8a740b2eb0355d91d3e36014e94a236eb78 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Mon, 6 Nov 2023 13:21:30 -0300 Subject: [PATCH] add the mobile phone #1847 for magento 2.2 --- Block/Subscribe.php | 25 +++ Helper/Data.php | 245 +++++++++++---------- Model/Plugin/Subscriber.php | 7 - Setup/UpgradeData.php | 46 +++- view/frontend/templates/footerwphone.phtml | 2 +- 5 files changed, 193 insertions(+), 132 deletions(-) diff --git a/Block/Subscribe.php b/Block/Subscribe.php index b4932db7..13926ec8 100644 --- a/Block/Subscribe.php +++ b/Block/Subscribe.php @@ -2,6 +2,8 @@ namespace Ebizmarts\MailChimp\Block; +use Magento\Customer\Model\Session; +use Magento\Customer\Model\CustomerFactory; use Magento\Framework\View\Element\Template; use \Ebizmarts\MailChimp\Helper\Data as MailchimpHelper; @@ -15,21 +17,29 @@ class Subscribe extends \Magento\Newsletter\Block\Subscribe * @var MailchimpHelper */ protected $helper; + protected $customerSession; + protected $customerFactory; /** * @param Template\Context $context * @param MailchimpHelper $helper + * @param Session $customerSession + * @param CustomerFactory $customerFactory * @param array $data */ public function __construct( Template\Context $context, MailchimpHelper $helper, + Session $customerSession, + CustomerFactory $customerFactory, array $data = [] ) { parent::__construct($context, $data); $this->context = $context; $this->helper = $helper; + $this->customerSession = $customerSession; + $this->customerFactory = $customerFactory; } public function getPopupUrl() @@ -42,4 +52,19 @@ 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; + } } \ No newline at end of file diff --git a/Helper/Data.php b/Helper/Data.php index 750e02d7..dd3d03f4 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -11,10 +11,33 @@ namespace Ebizmarts\MailChimp\Helper; -use Magento\Framework\App\ResourceConnection; -use Magento\Framework\Exception\ValidatorException; -use Magento\Store\Model\Store; +use Magento\Customer\Api\Data\CustomerInterface; use Symfony\Component\Config\Definition\Exception\Exception; +use Magento\Framework\App\ResourceConnection; +use Magento\Framework\App\DeploymentConfig; +use Magento\Framework\App\Cache\TypeListInterface; +use Magento\Framework\App\Helper\Context; +use Magento\Framework\Stdlib\DateTime\DateTime; +use Magento\Framework\Locale\Resolver; +use Magento\Framework\Serialize\Serializer\Json as JsonSerializer; +use Magento\Framework\Module\ModuleList\Loader; +use Magento\Framework\Encryption\Encryptor; +use Magento\Directory\Api\CountryInformationAcquirerInterface; +use Magento\Directory\Model\CountryFactory; +use Magento\Customer\Model\Session; +use Magento\Customer\Api\CustomerRepositoryInterface; +use Magento\Customer\Model\CustomerFactory; +use Magento\Customer\Model\ResourceModel\Attribute\CollectionFactory as AttributeCollectionFactory; +use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory; +use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory as SubscriberCollectionFactory; +use Magento\Store\Model\StoreManagerInterface; +use Magento\Config\Model\ResourceModel\Config; +use Ebizmarts\MailChimp\Model\Logger\Logger; +use Ebizmarts\MailChimp\Model\MailChimpSyncBatches; +use Ebizmarts\MailChimp\Model\MailChimpStoresFactory; +use Ebizmarts\MailChimp\Model\MailChimpStores; +use Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory; +use Magento\Framework\App\Config\ScopeConfigInterface; class Data extends \Magento\Framework\App\Helper\AbstractHelper { @@ -98,19 +121,15 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper protected $counters = []; /** - * @var \Magento\Store\Model\StoreManagerInterface + * @var StoreManagerInterface */ private $_storeManager; /** - * @var \Ebizmarts\MailChimp\Model\Logger\Logger + * @var Logger */ private $_mlogger; /** - * @var \Magento\Customer\Model\GroupRegistry - */ - private $_groupRegistry; - /** - * @var \Magento\Framework\App\Config\ScopeConfigInterface + * @var ScopeConfigInterface */ private $_scopeConfig; /** @@ -118,193 +137,177 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper */ protected $_request; /** - * @var \Magento\Framework\App\State - */ - private $_state; - /** - * @var \Magento\Framework\Module\ModuleList\Loader + * @var Loader */ private $_loader; /** - * @var \Magento\Config\Model\ResourceModel\Config + * @var Config */ private $_config; /** * @var \Mailchimp */ private $_api; - /** - * @var \Magento\Customer\Model\ResourceModel\Customer\CustomerRepository - */ - private $_customer; - /** - * @var \Ebizmarts\MailChimp\Model\MailChimpSyncBatches + * @var MailChimpSyncBatches */ private $_syncBatches; /** - * @var \Ebizmarts\MailChimp\Model\MailChimpStoresFactory + * @var MailChimpStoresFactory */ private $_mailChimpStoresFactory; /** - * @var \Ebizmarts\MailChimp\Model\MailChimpStores + * @var MailChimpStores */ private $_mailChimpStores; /** - * @var \Magento\Framework\Encryption\Encryptor + * @var Encryptor */ private $_encryptor; /** - * @var \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory + * @var SubscriberCollectionFactory */ private $_subscriberCollection; /** - * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory + * @var CustomerCollectionFactory */ private $_customerCollection; - private $_addressRepositoryInterface; - /** - * @var \Magento\Framework\DB\Adapter\AdapterInterface - */ - private $connection; /** - * @var \Magento\Framework\App\ResourceConnection + * @var ResourceConnection */ private $_resource; /** - * @var \Magento\Framework\App\Cache\TypeListInterface + * @var TypeListInterface */ private $_cacheTypeList; /** - * @var \Magento\Customer\Model\ResourceModel\Attribute\CollectionFactory + * @var AttributeCollectionFactory */ private $_attCollection; /** - * @var \Magento\Customer\Model\CustomerFactory + * @var CustomerFactory */ protected $_customerFactory; /** - * @var \Magento\Directory\Api\CountryInformationAcquirerInterface + * @var CountryInformationAcquirerInterface */ protected $_countryInformation; /** - * @var \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory + * @var MailChimpInterestGroupFactory */ protected $_interestGroupFactory; /** - * @var \Magento\Framework\Stdlib\DateTime\DateTime + * @var DateTime */ protected $_date; /** - * @var \Magento\Framework\Serialize\Serializer\Json + * @var JsonSerializer */ protected $_serializer; /** - * @var \Magento\Framework\App\DeploymentConfig + * @var DeploymentConfig */ protected $_deploymentConfig; /** - * @var \Magento\Directory\Model\CountryFactory + * @var CountryFactory */ protected $countryFactory; /** - * @var \Magento\Framework\Locale\Resolver + * @var CustomerRepositoryInterface + */ + protected $_customerRepo; + /** + * @var Session + */ + protected $customerSession; + /** + * @var CustomerFactory */ - protected $resolver; + protected $customerFactory; private $customerAtt = null; private $addressAtt = null; private $_mapFields = null; /** - * @param \Magento\Framework\App\Helper\Context $context - * @param \Magento\Store\Model\StoreManagerInterface $storeManager - * @param \Ebizmarts\MailChimp\Model\Logger\Logger $logger - * @param \Magento\Customer\Model\GroupRegistry $groupRegistry - * @param \Magento\Framework\App\State $state - * @param \Magento\Framework\Module\ModuleList\Loader $loader - * @param \Magento\Config\Model\ResourceModel\Config $config + * @param Context $context + * @param StoreManagerInterface $storeManager + * @param Logger $logger + * @param Loader $loader + * @param Config $config * @param \Mailchimp $api - * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList - * @param \Magento\Customer\Model\ResourceModel\CustomerRepository $customer - * @param \Ebizmarts\MailChimp\Model\MailChimpSyncBatches $syncBatches - * @param \Ebizmarts\MailChimp\Model\MailChimpStoresFactory $mailChimpStoresFactory - * @param \Ebizmarts\MailChimp\Model\MailChimpStores $mailChimpStores - * @param \Magento\Customer\Model\ResourceModel\Attribute\CollectionFactory $attCollection - * @param \Magento\Framework\Encryption\Encryptor $encryptor - * @param \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollection - * @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollection - * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepositoryInterface - * @param \Magento\Customer\Model\CustomerFactory $customerFactory - * @param \Magento\Directory\Api\CountryInformationAcquirerInterface $countryInformation + * @param TypeListInterface $cacheTypeList + * @param MailChimpSyncBatches $syncBatches + * @param MailChimpStoresFactory $mailChimpStoresFactory + * @param MailChimpStores $mailChimpStores + * @param AttributeCollectionFactory $attCollection + * @param Encryptor $encryptor + * @param SubscriberCollectionFactory $subscriberCollection + * @param CustomerCollectionFactory $customerCollection + * @param CustomerRepositoryInterface $customerRepository + * @param CountryInformationAcquirerInterface $countryInformation * @param ResourceConnection $resource - * @param \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory - * @param \Magento\Framework\Serialize\Serializer\Json $serializer - * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig - * @param \Magento\Framework\Stdlib\DateTime\DateTime $date - * @param \Magento\Directory\Model\CountryFactory $countryFactory - * @param \Magento\Framework\Locale\Resolver $resolver + * @param MailChimpInterestGroupFactory $interestGroupFactory + * @param JsonSerializer $serializer + * @param DeploymentConfig $deploymentConfig + * @param DateTime $date + * @param CountryFactory $countryFactory + * @param Session $customerSession + * @param CustomerFactory $customerFactory */ public function __construct( - \Magento\Framework\App\Helper\Context $context, - \Magento\Store\Model\StoreManagerInterface $storeManager, - \Ebizmarts\MailChimp\Model\Logger\Logger $logger, - \Magento\Customer\Model\GroupRegistry $groupRegistry, - \Magento\Framework\App\State $state, - \Magento\Framework\Module\ModuleList\Loader $loader, - \Magento\Config\Model\ResourceModel\Config $config, + Context $context, + StoreManagerInterface $storeManager, + Logger $logger, + Loader $loader, + Config $config, \Mailchimp $api, - \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, - \Magento\Customer\Model\ResourceModel\CustomerRepository $customer, - \Ebizmarts\MailChimp\Model\MailChimpSyncBatches $syncBatches, - \Ebizmarts\MailChimp\Model\MailChimpStoresFactory $mailChimpStoresFactory, - \Ebizmarts\MailChimp\Model\MailChimpStores $mailChimpStores, - \Magento\Customer\Model\ResourceModel\Attribute\CollectionFactory $attCollection, - \Magento\Framework\Encryption\Encryptor $encryptor, - \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollection, - \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollection, - \Magento\Customer\Api\AddressRepositoryInterface $addressRepositoryInterface, - \Magento\Customer\Model\CustomerFactory $customerFactory, - \Magento\Directory\Api\CountryInformationAcquirerInterface $countryInformation, - \Magento\Framework\App\ResourceConnection $resource, - \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory, - \Magento\Framework\Serialize\Serializer\Json $serializer, - \Magento\Framework\App\DeploymentConfig $deploymentConfig, - \Magento\Framework\Stdlib\DateTime\DateTime $date, - \Magento\Directory\Model\CountryFactory $countryFactory, - \Magento\Framework\Locale\Resolver $resolver + TypeListInterface $cacheTypeList, + MailChimpSyncBatches $syncBatches, + MailChimpStoresFactory $mailChimpStoresFactory, + MailChimpStores $mailChimpStores, + AttributeCollectionFactory $attCollection, + Encryptor $encryptor, + SubscriberCollectionFactory $subscriberCollection, + CustomerCollectionFactory $customerCollection, + CustomerRepositoryInterface $customerRepository, + CountryInformationAcquirerInterface $countryInformation, + ResourceConnection $resource, + MailChimpInterestGroupFactory $interestGroupFactory, + JsonSerializer $serializer, + DeploymentConfig $deploymentConfig, + DateTime $date, + CountryFactory $countryFactory, + Session $customerSession, + CustomerFactory $customerFactory ) { - $this->_storeManager = $storeManager; $this->_mlogger = $logger; - $this->_groupRegistry = $groupRegistry; $this->_scopeConfig = $context->getScopeConfig(); $this->_request = $context->getRequest(); - $this->_state = $state; $this->_loader = $loader; $this->_config = $config; $this->_api = $api; - $this->_customer = $customer; $this->_syncBatches = $syncBatches; $this->_mailChimpStores = $mailChimpStores; $this->_mailChimpStoresFactory = $mailChimpStoresFactory; $this->_encryptor = $encryptor; $this->_subscriberCollection = $subscriberCollection; $this->_customerCollection = $customerCollection; - $this->_addressRepositoryInterface = $addressRepositoryInterface; $this->_resource = $resource; - $this->connection = $resource->getConnection(); $this->_cacheTypeList = $cacheTypeList; $this->_attCollection = $attCollection; + $this->_customerRepo = $customerRepository; $this->_customerFactory = $customerFactory; $this->_countryInformation = $countryInformation; $this->_interestGroupFactory = $interestGroupFactory; $this->_serializer = $serializer; $this->_deploymentConfig = $deploymentConfig; - $this->_date = $date; + $this->_date = $date; $this->countryFactory = $countryFactory; - $this->resolver = $resolver; + $this->customerSession = $customerSession; + $this->customerFactory = $customerFactory; parent::__construct($context); } @@ -775,8 +778,7 @@ 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(); @@ -787,21 +789,28 @@ public function getMergeVarsBySubscriber(\Magento\Newsletter\Model\Subscriber $s if (!$email) { $email = $subscriber->getEmail(); } - try { - /** - * @var $customer \Magento\Customer\Model\Customer - */ - $customer = $this->_customerFactory->create(); - $customer->setWebsiteId($webSiteId); - $customer->loadByEmail($email); - if ($customer->getData('email') == $email) { - $mergeVars = array_merge($mergeVars,$this->getMergeVars($customer, $customer->getStoreId())); + if ($this->customerSession->getCustomerId()) { + try { + /** + * @var $customer CustomerInterface + */ + $customer = $this->customerFactory->create()->load($this->customerSession->getCustomerId()); + $this->log("Customer ".$customer->getId()); + if ($customer->getData('mobile_phone')) { + $this->log($customer->getData('mobile_phone')); + } else { + $this->log('no mobile phone'); + } + if ($customer->getData('email') == $email) { + $mergeVars = array_merge($mergeVars, $this->getMergeVars($customer, $customer->getStoreId())); + } + } catch (\Exception $e) { + $this->log($e->getMessage()); } - } catch (\Exception $e) { - $this->log($e->getMessage()); + } else { + $this->log("Subscriber is not a customer"); } - return $mergeVars; - } + return $mergeVars; } /** * @param \Magento\Customer\Model\Customer $customer diff --git a/Model/Plugin/Subscriber.php b/Model/Plugin/Subscriber.php index 36aa9f06..7f4edbd6 100644 --- a/Model/Plugin/Subscriber.php +++ b/Model/Plugin/Subscriber.php @@ -29,13 +29,6 @@ class Subscriber * @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 diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php index 02bf6146..7e3a998b 100644 --- a/Setup/UpgradeData.php +++ b/Setup/UpgradeData.php @@ -12,14 +12,16 @@ */ namespace Ebizmarts\MailChimp\Setup; -use Magento\Config\Model\Config; -use Magento\Config\Model\ResourceModel\Config\Data\Collection; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Framework\App\ResourceConnection; use Magento\Framework\App\DeploymentConfig; use Magento\Sales\Model\OrderFactory; +use Magento\Customer\Setup\CustomerSetupFactory; +use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; +use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet; +use Magento\Customer\Model\Customer; use Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory as SyncCollectionFactory; class UpgradeData implements UpgradeDataInterface @@ -53,9 +55,14 @@ class UpgradeData implements UpgradeDataInterface */ private $syncCollectionFactory; /** - * @var OrderFactory + * @var CustomerSetupFactory */ - private $orderFactory; + protected $customerSetupFactory; + /** + * @var AttributeSetFactory + */ + private $attributeSetFactory; + /** * @param ResourceConnection $resource @@ -74,7 +81,8 @@ public function __construct( \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpWebhookRequest\CollectionFactory $webhookCollectionFactory, \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configFactory, SyncCollectionFactory $syncCollectionFactory, - OrderFactory $orderFactory, + CustomerSetupFactory $customerSetupFactory, + AttributeSetFactory $attributeSetFactory, \Ebizmarts\MailChimp\Helper\Data $helper ) { $this->_resource = $resource; @@ -83,7 +91,8 @@ public function __construct( $this->_webhookCollectionFactory = $webhookCollectionFactory; $this->configFactory = $configFactory; $this->syncCollectionFactory = $syncCollectionFactory; - $this->orderFactory = $orderFactory; + $this->customerSetupFactory = $customerSetupFactory; + $this->attributeSetFactory = $attributeSetFactory; $this->_helper = $helper; } @@ -183,5 +192,30 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $config->getResource()->delete($config); } } + if (version_compare($context->getVersion(), '101.2.58')){ + $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); + $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); + $attributeSetId = $customerEntity->getDefaultAttributeSetId(); + /** @var $attributeSet AttributeSet */ + $attributeSet = $this->attributeSetFactory->create(); + $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); + $customerSetup->addAttribute(Customer::ENTITY, 'mobile_phone', [ + 'type' => 'varchar', + 'label' => 'Mobile Phone', + 'input' => 'text', + 'required' => false, + 'visible' => true, + 'user_defined' => true, + 'position' => 999, + 'system' => 0, + ]); + $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile_phone') + ->addData([ + 'attribute_set_id' => $attributeSetId, + 'attribute_group_id' => $attributeGroupId, + 'used_in_forms' => ['adminhtml_customer', 'customer_account_edit'], + ]); + $attribute->save(); + } } } diff --git a/view/frontend/templates/footerwphone.phtml b/view/frontend/templates/footerwphone.phtml index 731d0f69..bf07f0a2 100644 --- a/view/frontend/templates/footerwphone.phtml +++ b/view/frontend/templates/footerwphone.phtml @@ -21,7 +21,7 @@