contactFactory = $contactFactory; $this->helper = $data; $this->storeManager = $storeManagerInterface; $this->importerFactory = $importerFactory; $this->contactResource = $contactResource; } /** * Remove contact from account * * @param \Magento\Framework\Event\Observer $observer * * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); $email = $subscriber->getEmail(); $websiteId = $this->storeManager->getStore($subscriber->getStoreId()) ->getWebsiteId(); $apiEnabled = $this->helper->isEnabled($websiteId); /* * Remove contact. */ if ($apiEnabled) { try { //register in queue with importer $this->importerFactory->create()->registerQueue( \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_CONTACT, $email, \Dotdigitalgroup\Email\Model\Importer::MODE_CONTACT_DELETE, $websiteId ); $contactModel = $this->contactFactory->create() ->loadByCustomerEmail($email, $websiteId); if ($contactModel->getId()) { //remove contact $this->contactResource->delete($contactModel); } } catch (\Exception $e) { $this->helper->debug((string)$e, []); } } return $this; } }