http = $http; $this->header = $header; $this->redirect = $redirect; $this->helper = $data; $this->configHelper = $this->helper->configHelperFactory->create(); $this->timezone = $timezone; $this->storeManager = $storeManagerInterface; $this->consentFactory = $consentFactory; $this->contactFactory = $contactFactory; $this->consentResource = $consentResource; } /** * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); $email = $subscriber->getEmail(); $subscriberStatus = $subscriber->getSubscriberStatus(); $websiteId = $this->storeManager->getStore($subscriber->getStoreId()) ->getWebsiteId(); //If not confirmed or not enabled. if ($subscriberStatus == \Magento\Newsletter\Model\Subscriber::STATUS_UNSUBSCRIBED || !$this->helper->isEnabled($websiteId) || !$this->configHelper->isConsentSubscriberEnabled($websiteId) ) { return $this; } try { $contactEmail = $this->contactFactory->create() ->loadByCustomerEmail($email, $websiteId); $emailContactId = $contactEmail->getId(); $consentModel = $this->consentFactory->create(); $this->consentResource->load( $consentModel, $emailContactId, 'email_contact_id' ); //don't update the consent data for guest subscribers or not confrimed if (! $consentModel->isObjectNew()) { return $this; } $consentIp = $this->http->getClientIp(); $consentUrl = $this->redirect->getRefererUrl(); $consentUserAgent = $this->header->getHttpUserAgent(); //save the consent data against the contact $consentModel->setEmailContactId($emailContactId) ->setConsentUrl($consentUrl) ->setConsentDatetime(time()) ->setConsentIp($consentIp) ->setConsentUserAgent($consentUserAgent); //save contact $this->consentResource->save($consentModel); } catch (\Exception $e) { $this->helper->debug((string)$e, []); } return $this; } }