wishlist = $wishlist; $this->contactFactory = $contactFactory; $this->contactResource = $contactResource; $this->customerFactory = $customerFactory; $this->helper = $data; $this->registry = $registry; $this->importerFactory = $importerFactory; $this->subscriberFactory = $subscriberFactory; } /** * If it's configured to capture on shipment - do this. * * @param \Magento\Framework\Event\Observer $observer * * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { $customer = $observer->getEvent()->getCustomer(); $websiteId = $customer->getWebsiteId(); $storeId = $customer->getStoreId(); //check if enabled if (!$this->helper->isEnabled($websiteId)) { return $this; } $email = $customer->getEmail(); $customerId = $customer->getEntityId(); $subscriber = $this->subscriberFactory->create() ->loadByCustomerId($customerId); $isSubscribed = $subscriber->isSubscribed(); $emailBefore = ''; try { // fix for a multiple hit of the observer $emailReg = $this->registry->registry($email . '_customer_save'); if ($emailReg) { return $this; } $this->registry->unregister($email . '_customer_save'); // additional measure $this->registry->register($email . '_customer_save', $email); $isContactExist = $this->contactFactory->create() ->loadByCustomerId($customerId); if ($isContactExist->getId()) { $emailBefore = $isContactExist->getEmail(); } $emailAddress = empty($emailBefore) ? $email : $emailBefore; $contactModel = $this->contactFactory->create() ->loadByCustomerEmail($emailAddress, $websiteId); //email change detection if ($emailBefore && $email != $emailBefore) { //Reload contact model up update email $contactModel = $this->contactFactory->create() ->loadByCustomerEmail($emailAddress, $websiteId); $contactModel->setEmail($email); $data = [ 'emailBefore' => $emailBefore, 'email' => $email, 'isSubscribed' => $isSubscribed, ]; $this->importerFactory->create()->registerQueue( \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_CONTACT_UPDATE, $data, \Dotdigitalgroup\Email\Model\Importer::MODE_CONTACT_EMAIL_UPDATE, $websiteId ); } elseif (!$emailBefore) { //for new contacts update email $contactModel->setEmail($email); } $contactModel->setEmailImported(\Dotdigitalgroup\Email\Model\Contact::EMAIL_CONTACT_NOT_IMPORTED) ->setStoreId($storeId) ->setCustomerId($customerId); $this->contactResource->save($contactModel); } catch (\Exception $e) { $this->helper->debug((string)$e, []); } return $this; } }