From 8f3025d9f8014319296c145980281cf839830927 Mon Sep 17 00:00:00 2001 From: bnayal Date: Wed, 31 Jan 2018 17:11:08 +0200 Subject: [PATCH] 2.2.12 - identifu accepts_marketing status for new customers registrations --- Model/Api/Data.php | 2 +- Observer/EventMethods.php | 7 +++++-- ...riggerCustomerAddressBeforeUpdateObserver.php | 13 ------------- .../TriggerCustomerAddressUpdateObserver.php | 12 ------------ Observer/TriggerCustomerDeleteObserver.php | 12 ------------ Observer/TriggerCustomerUpdateObserver.php | 12 +----------- Observer/TriggerSubscribeDeleteObserver.php | 16 +++------------- Observer/TriggerSubscribeUpdateObserver.php | 10 +++++++++- Serializer/CustomerSerializer.php | 15 ++++++++++++--- composer.json | 2 +- etc/module.xml | 2 +- 11 files changed, 33 insertions(+), 70 deletions(-) diff --git a/Model/Api/Data.php b/Model/Api/Data.php index 5622da2..84d794d 100644 --- a/Model/Api/Data.php +++ b/Model/Api/Data.php @@ -1206,7 +1206,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue) */ public function getVersion() { - return '2.2.11'; + return '2.2.12'; } /** diff --git a/Observer/EventMethods.php b/Observer/EventMethods.php index 4dc6887..267e93f 100644 --- a/Observer/EventMethods.php +++ b/Observer/EventMethods.php @@ -9,7 +9,6 @@ use \Magento\Customer\Model\Group; use Magento\Store\Model\StoreManager; use Remarkety\Mgconnector\Helper\ConfigHelper; -use \Remarkety\Mgconnector\Model\Queue; use \Magento\Store\Model\Store; use \Magento\Framework\UrlInterface; use \Magento\Framework\App\Config\ScopeConfigInterface; @@ -283,7 +282,7 @@ protected function _getPayloadBase($eventType) } - protected function _prepareCustomerSubscribtionUpdateData(Subscriber $subscriber, $clientIp = null) + protected function _prepareCustomerSubscribtionUpdateData(Subscriber $subscriber, $clientIp = null, $customerId = null) { $arr = array( 'email' => $subscriber->getSubscriberEmail(), @@ -295,6 +294,10 @@ protected function _prepareCustomerSubscribtionUpdateData(Subscriber $subscriber $arr['client_ip'] = $clientIp; } + if(!empty($customerId)){ + $arr['customerId'] = $customerId; + } + return $arr; } diff --git a/Observer/TriggerCustomerAddressBeforeUpdateObserver.php b/Observer/TriggerCustomerAddressBeforeUpdateObserver.php index d84666f..017165d 100644 --- a/Observer/TriggerCustomerAddressBeforeUpdateObserver.php +++ b/Observer/TriggerCustomerAddressBeforeUpdateObserver.php @@ -3,20 +3,7 @@ namespace Remarkety\Mgconnector\Observer; use Magento\Framework\Event\ObserverInterface; -use Magento\Customer\Model\Address; use Magento\Customer\Model\CustomerFactory; -use \Magento\Framework\ObjectManager\ObjectManager; -use \Magento\Framework\Registry; -use Remarkety\Mgconnector\Helper\ConfigHelper; -use Remarkety\Mgconnector\Observer\EventMethods; -use \Magento\Newsletter\Model\Subscriber; -use \Magento\Customer\Model\Group; -use \Remarkety\Mgconnector\Model\Queue; -use \Magento\Store\Model\Store; -use \Magento\Framework\App\Config\ScopeConfigInterface; -use Remarkety\Mgconnector\Serializer\AddressSerializer; -use Remarkety\Mgconnector\Serializer\CustomerSerializer; -use Remarkety\Mgconnector\Serializer\OrderSerializer; class TriggerCustomerAddressBeforeUpdateObserver extends EventMethods implements ObserverInterface diff --git a/Observer/TriggerCustomerAddressUpdateObserver.php b/Observer/TriggerCustomerAddressUpdateObserver.php index 69d8b37..ecce3df 100644 --- a/Observer/TriggerCustomerAddressUpdateObserver.php +++ b/Observer/TriggerCustomerAddressUpdateObserver.php @@ -5,18 +5,6 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Customer\Model\Address; use Magento\Customer\Model\CustomerFactory; -use \Magento\Framework\Registry; -use \Magento\Framework\ObjectManager\ObjectManager; -use Remarkety\Mgconnector\Helper\ConfigHelper; -use Remarkety\Mgconnector\Observer\EventMethods; -use \Magento\Newsletter\Model\Subscriber; -use \Magento\Customer\Model\Group; -use \Remarkety\Mgconnector\Model\Queue; -use \Magento\Store\Model\Store; -use \Magento\Framework\App\Config\ScopeConfigInterface; -use Remarkety\Mgconnector\Serializer\AddressSerializer; -use Remarkety\Mgconnector\Serializer\CustomerSerializer; -use Remarkety\Mgconnector\Serializer\OrderSerializer; class TriggerCustomerAddressUpdateObserver extends EventMethods implements ObserverInterface { diff --git a/Observer/TriggerCustomerDeleteObserver.php b/Observer/TriggerCustomerDeleteObserver.php index b385ada..f2086b4 100644 --- a/Observer/TriggerCustomerDeleteObserver.php +++ b/Observer/TriggerCustomerDeleteObserver.php @@ -4,18 +4,6 @@ use Magento\Customer\Model\Backend\Customer; use Magento\Framework\Event\ObserverInterface; -use Remarkety\Mgconnector\Helper\ConfigHelper; -use Remarkety\Mgconnector\Observer\EventMethods; -use \Magento\Customer\Model\Session; -use \Magento\Framework\Registry; -use \Magento\Newsletter\Model\Subscriber; -use \Magento\Customer\Model\Group; -use \Remarkety\Mgconnector\Model\Queue; -use \Magento\Store\Model\Store; -use \Magento\Framework\App\Config\ScopeConfigInterface; -use Remarkety\Mgconnector\Serializer\AddressSerializer; -use Remarkety\Mgconnector\Serializer\CustomerSerializer; -use Remarkety\Mgconnector\Serializer\OrderSerializer; class TriggerCustomerDeleteObserver extends EventMethods implements ObserverInterface { diff --git a/Observer/TriggerCustomerUpdateObserver.php b/Observer/TriggerCustomerUpdateObserver.php index 74b60a7..c60ca05 100644 --- a/Observer/TriggerCustomerUpdateObserver.php +++ b/Observer/TriggerCustomerUpdateObserver.php @@ -3,18 +3,7 @@ namespace Remarkety\Mgconnector\Observer; use Magento\Framework\Event\ObserverInterface; -use Magento\Customer\Model\Address; use Magento\Customer\Model\CustomerFactory; -use \Magento\Framework\Registry; -use \Magento\Newsletter\Model\Subscriber; -use \Magento\Customer\Model\Group; -use Remarkety\Mgconnector\Helper\ConfigHelper; -use \Remarkety\Mgconnector\Model\Queue; -use \Magento\Store\Model\Store; -use \Magento\Framework\App\Config\ScopeConfigInterface; -use Remarkety\Mgconnector\Serializer\AddressSerializer; -use Remarkety\Mgconnector\Serializer\CustomerSerializer; -use Remarkety\Mgconnector\Serializer\OrderSerializer; class TriggerCustomerUpdateObserver extends EventMethods implements ObserverInterface @@ -40,6 +29,7 @@ public function execute(\Magento\Framework\Event\Observer $observer) */ $customer = $backendModel->getDataModel(); $customerOld = $this->customerRepository->getById($customer->getId()); + $this->_coreRegistry->register('remarkety_customer_id', $customer->getId(), true); if($this->_coreRegistry->registry('remarkety_customer_save_observer_executed_'.$customer->getId()) || !$customer->getId()) { return $this; diff --git a/Observer/TriggerSubscribeDeleteObserver.php b/Observer/TriggerSubscribeDeleteObserver.php index 1e79783..11b3701 100644 --- a/Observer/TriggerSubscribeDeleteObserver.php +++ b/Observer/TriggerSubscribeDeleteObserver.php @@ -3,18 +3,6 @@ namespace Remarkety\Mgconnector\Observer; use Magento\Framework\Event\ObserverInterface; -use \Magento\Customer\Model\Session; -use \Magento\Framework\Registry; -use \Magento\Newsletter\Model\Subscriber; -use \Magento\Customer\Model\Group; -use Remarkety\Mgconnector\Helper\ConfigHelper; -use \Remarkety\Mgconnector\Model\Queue; -use \Magento\Store\Model\Store; -use Magento\Store\Model\StoreManager; -use \Magento\Framework\App\Config\ScopeConfigInterface; -use Remarkety\Mgconnector\Serializer\AddressSerializer; -use Remarkety\Mgconnector\Serializer\CustomerSerializer; -use Remarkety\Mgconnector\Serializer\OrderSerializer; class TriggerSubscribeDeleteObserver extends EventMethods implements ObserverInterface { @@ -22,12 +10,14 @@ public function execute(\Magento\Framework\Event\Observer $observer) { try { $subscriber = $observer->getEvent()->getSubscriber(); - if (!$this->_coreRegistry->registry('remarkety_subscriber_deleted_' . $subscriber->getEmail()) && $subscriber->getId()) { + $regKey = 'remarkety_subscriber_deleted_' . $subscriber->getEmail(); + if (!$this->_coreRegistry->registry($regKey) && $subscriber->getId()) { $this->makeRequest( 'newsletter/unsubscribed', $this->_prepareCustomerSubscribtionDeleteData($subscriber), $subscriber->getStoreId() ); + $this->_coreRegistry->register($regKey, 1, true); } } catch (\Exception $ex){ $this->logError($ex); diff --git a/Observer/TriggerSubscribeUpdateObserver.php b/Observer/TriggerSubscribeUpdateObserver.php index d994a48..d4ff60f 100644 --- a/Observer/TriggerSubscribeUpdateObserver.php +++ b/Observer/TriggerSubscribeUpdateObserver.php @@ -82,6 +82,8 @@ public function execute(\Magento\Framework\Event\Observer $observer){ if($subscriber->getId()) { if ($this->_coreRegistry->registry('remarkety_subscriber_deleted_' . $subscriber->getSubscriberEmail())) return $this; + if ($this->_coreRegistry->registry('remarkety_subscriber_updated_' . $subscriber->getSubscriberEmail())) + return $this; $status = $subscriber->getStatus(); $eventType = 'newsletter/subscribed'; @@ -98,7 +100,12 @@ public function execute(\Magento\Framework\Event\Observer $observer){ return $this; } - $this->makeRequest($eventType, $this->_prepareCustomerSubscribtionUpdateData($subscriber, $this->remoteAddress->getRemoteAddress()), $subscriber->getStoreId()); + $customerId = $this->session->getCustomerId(); + if(empty($customerId)){ + $customerId = $this->_coreRegistry->registry('remarkety_customer_id'); + } + $data = $this->_prepareCustomerSubscribtionUpdateData($subscriber, $this->remoteAddress->getRemoteAddress(), $customerId); + $this->makeRequest($eventType, $data, $subscriber->getStoreId()); if($this->_store->getId() != 0){ $email = $subscriber->getSubscriberEmail(); @@ -113,6 +120,7 @@ public function execute(\Magento\Framework\Event\Observer $observer){ } } } + $this->_coreRegistry->register('remarkety_subscriber_updated_' . $subscriber->getSubscriberEmail(), 1, true); } } catch (\Exception $ex){ $this->logError($ex); diff --git a/Serializer/CustomerSerializer.php b/Serializer/CustomerSerializer.php index 96f660f..3125ba4 100644 --- a/Serializer/CustomerSerializer.php +++ b/Serializer/CustomerSerializer.php @@ -9,6 +9,7 @@ namespace Remarkety\Mgconnector\Serializer; +use Magento\Framework\App\RequestInterface; use Magento\Newsletter\Model\Subscriber; use Magento\Customer\Api\GroupRepositoryInterface as CustomerGroupRepository; @@ -18,18 +19,26 @@ class CustomerSerializer private $subscriber; private $addressSerializer; private $customerGroupRepository; + private $request; public function __construct( Subscriber $subscriber, AddressSerializer $addressSerializer, - CustomerGroupRepository $customerGroupRepository + CustomerGroupRepository $customerGroupRepository, + RequestInterface $request ) { $this->subscriber = $subscriber; $this->addressSerializer = $addressSerializer; $this->customerGroupRepository = $customerGroupRepository; + $this->request = $request; } public function serialize(\Magento\Customer\Api\Data\CustomerInterface $customer){ - $checkSubscriber = $this->subscriber->loadByEmail($customer->getEmail()); + if ($this->request->getParam('is_subscribed', false)) { + $subscribed = true; + } else { + $checkSubscriber = $this->subscriber->loadByEmail($customer->getEmail()); + $subscribed = $checkSubscriber->isSubscribed(); + } $created_at = new \DateTime($customer->getCreatedAt()); $updated_at = new \DateTime($customer->getUpdatedAt()); @@ -56,7 +65,7 @@ public function serialize(\Magento\Customer\Api\Data\CustomerInterface $customer $customerInfo = [ 'id' => (int)$customer->getId(), 'email' => $customer->getEmail(), - 'accepts_marketing' => $checkSubscriber->isSubscribed(), + 'accepts_marketing' => $subscribed, 'title' => $customer->getPrefix(), 'first_name' => $customer->getFirstname(), 'last_name' => $customer->getLastname(), diff --git a/composer.json b/composer.json index 6c368a4..337bacf 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "lib-libxml": "*" }, "type": "magento2-module", - "version": "2.2.11", + "version": "2.2.12", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/etc/module.xml b/etc/module.xml index 290e44e..295fa4d 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - +