Observer/Newsletter/ChangeContactSubscription.php
public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $subscriber = $observer->getEvent()->getSubscriber();
        $this->isSubscriberNew = $subscriber->isObjectNew();
        $email = $subscriber->getEmail();
        $storeId = $subscriber->getStoreId();
        $subscriberStatus = $subscriber->getSubscriberStatus();
        $websiteId = $this->storeManager->getStore($subscriber->getStoreId())
            ->getWebsiteId();

        //api is enabled
        if (!$this->helper->isEnabled($websiteId)) {
            return $this;
        }

        try {
            $contactEmail = $this->contactFactory->create()
                ->loadByCustomerEmail($email, $websiteId);

            //update the contact
            $contactEmail->setStoreId($storeId)
                ->setSubscriberStatus($subscriberStatus);

            // only for subscribers
            if ($subscriberStatus == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED) {
                //Set contact as subscribed
                $contactEmail->setSubscriberImported(null)
                    ->setIsSubscriber('1');

                //Subscriber subscribed when it is suppressed in table then re-subscribe
                if ($contactEmail->getSuppressed()) {
                    $this->importerFactory->registerQueue(
                        \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_SUBSCRIBER_RESUBSCRIBED,
                        ['email' => $email],
                        \Dotdigitalgroup\Email\Model\Importer::MODE_SUBSCRIBER_RESUBSCRIBED,
                        $websiteId
                    );
                    //Set to subscriber imported and reset the subscriber as suppressed
                    $contactEmail->setSubscriberImported(1)
                        ->setSuppressed(null);
                }
                //save contact
                $this->contactResource->save($contactEmail);

                //not subscribed
            } else {
                //skip if contact is suppressed
                if ($contactEmail->getSuppressed()) {
                    return $this;
                }
                $contactEmail->setSubscriberImported(1)
                    ->setIsSubscriber(null);
                //save contact
                $this->contactResource->save($contactEmail);

                //need to confirm enabled, to keep before the subscription data for contentinsight.
                if ($subscriberStatus == \Magento\Newsletter\Model\Subscriber::STATUS_UNCONFIRMED ||
                    $subscriberStatus == \Magento\Newsletter\Model\Subscriber::STATUS_NOT_ACTIVE) {
                    return $this;
                }

                //Add subscriber update to importer queue
                $this->importerFactory->registerQueue(
                    \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_SUBSCRIBER_UPDATE,
                    ['email' => $email, 'id' => $contactEmail->getId()],
                    \Dotdigitalgroup\Email\Model\Importer::MODE_SUBSCRIBER_UPDATE,
                    $websiteId
                );
            }

            // fix for a multiple hit of the observer. stop adding the duplicates on the automation
            $emailReg = $this->registry->registry($email . '_subscriber_save');
            if ($emailReg) {
                return $this;
            }
            $this->registry->unregister($email . '_subscriber_save'); // additional measure
            $this->registry->register($email . '_subscriber_save', $email);
            //add subscriber to automation
            $this->addSubscriberToAutomation($email, $subscriber, $websiteId);
        } catch (\Exception $e) {
            $this->helper->debug((string)$e, []);
        }

        return $this;
    }
public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $subscriber = $observer->getEvent()->getSubscriber();
        $this->isSubscriberNew = $subscriber->isObjectNew();
        $email = $subscriber->getEmail();
        $storeId = $subscriber->getStoreId();
        $subscriberStatus = $subscriber->getSubscriberStatus();
        $websiteId = $this->storeManager->getStore($subscriber->getStoreId())
            ->getWebsiteId();

        //api is enabled
        if (!$this->helper->isEnabled($websiteId)) {
            return $this;
        }

        try {
            $contactEmail = $this->contactFactory->create()
                ->loadByCustomerEmail($email, $websiteId);

            //update the contact
            $contactEmail->setStoreId($storeId)
                ->setSubscriberStatus($subscriberStatus);

            // only for subscribers
            if ($subscriberStatus == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED) {
                //Set contact as subscribed
                $contactEmail->setSubscriberImported(null)
                    ->setIsSubscriber('1');

                //Subscriber subscribed when it is suppressed in table then re-subscribe
                if ($contactEmail->getSuppressed()) {
                    $this->importerFactory->registerQueue(
                        \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_SUBSCRIBER_RESUBSCRIBED,
                        ['email' => $email],
                        \Dotdigitalgroup\Email\Model\Importer::MODE_SUBSCRIBER_RESUBSCRIBED,
                        $websiteId
                    );
                    //Set to subscriber imported and reset the subscriber as suppressed
                    $contactEmail->setSubscriberImported(1)
                        ->setSuppressed(null);
                }
                //save contact
                $this->contactResource->save($contactEmail);

                //not subscribed
            } else {
                //skip if contact is suppressed
                if ($contactEmail->getSuppressed()) {
                    return $this;
                }
                $contactEmail->setSubscriberImported(1)
                    ->setIsSubscriber(null);
                //save contact
                $this->contactResource->save($contactEmail);

                //need to confirm enabled, to keep before the subscription data for contentinsight.
                if ($subscriberStatus == \Magento\Newsletter\Model\Subscriber::STATUS_UNCONFIRMED ||
                    $subscriberStatus == \Magento\Newsletter\Model\Subscriber::STATUS_NOT_ACTIVE) {
                    return $this;
                }

                //Add subscriber update to importer queue
                $this->importerFactory->registerQueue(
                    \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_SUBSCRIBER_UPDATE,
                    ['email' => $email, 'id' => $contactEmail->getId()],
                    \Dotdigitalgroup\Email\Model\Importer::MODE_SUBSCRIBER_UPDATE,
                    $websiteId
                );
            }

            // fix for a multiple hit of the observer. stop adding the duplicates on the automation
            $emailReg = $this->registry->registry($email . '_subscriber_save');
            if ($emailReg) {
                return $this;
            }
            $this->registry->unregister($email . '_subscriber_save'); // additional measure
            $this->registry->register($email . '_subscriber_save', $email);
            //add subscriber to automation
            $this->addSubscriberToAutomation($email, $subscriber, $websiteId);
        } catch (\Exception $e) {
            $this->helper->debug((string)$e, []);
        }

        return $this;
    }
public function execute ( \ Magento \ Framework \ Event \ Observer $ observer ) { $ subscriber = $ observer -> getEvent ( ) -> getSubscriber ( ) ; $ this -> isSubscriberNew = $ subscriber -> isObjectNew ( ) ; $ email = $ subscriber -> getEmail ( ) ; $ storeId = $ subscriber -> getStoreId ( ) ; $ subscriberStatus = $ subscriber -> getSubscriberStatus ( ) ; $ websiteId = $ this -> storeManager -> getStore ( $ subscriber -> getStoreId ( ) ) -> getWebsiteId ( ) ; //api is enabled if ( ! $ this -> helper -> isEnabled ( $ websiteId ) ) { return $ this ; } try { $ contactEmail = $ this -> contactFactory -> create ( ) -> loadByCustomerEmail ( $ email , $ websiteId ) ; //update the contact $ contactEmail -> setStoreId ( $ storeId ) -> setSubscriberStatus ( $ subscriberStatus ) ; // only for subscribers if ( $ subscriberStatus == \ Magento \ Newsletter \ Model \ Subscriber :: STATUS_SUBSCRIBED ) { //Set contact as subscribed $ contactEmail -> setSubscriberImported ( null ) -> setIsSubscriber ( '1' ) ; //Subscriber subscribed when it is suppressed in table then re-subscribe if ( $ contactEmail -> getSuppressed ( ) ) { $ this -> importerFactory -> registerQueue ( \ Dotdigitalgroup \ Email \ Model \ Importer :: IMPORT_TYPE_SUBSCRIBER_RESUBSCRIBED , [ 'email' => $ email ] , \ Dotdigitalgroup \ Email \ Model \ Importer :: MODE_SUBSCRIBER_RESUBSCRIBED , $ websiteId ) ; //Set to subscriber imported and reset the subscriber as suppressed $ contactEmail -> setSubscriberImported ( 1 ) -> setSuppressed ( null ) ; } //save contact $ this -> contactResource -> save ( $ contactEmail ) ; //not subscribed } else { //skip if contact is suppressed if ( $ contactEmail -> getSuppressed ( ) ) { return $ this ; } $ contactEmail -> setSubscriberImported ( 1 ) -> setIsSubscriber ( null ) ; //save contact $ this -> contactResource -> save ( $ contactEmail ) ; //need to confirm enabled, to keep before the subscription data for contentinsight. if ( $ subscriberStatus == \ Magento \ Newsletter \ Model \ Subscriber :: STATUS_UNCONFIRMED || $ subscriberStatus == \ Magento \ Newsletter \ Model \ Subscriber :: STATUS_NOT_ACTIVE ) { return $ this ; } //Add subscriber update to importer queue $ this -> importerFactory -> registerQueue ( \ Dotdigitalgroup \ Email \ Model \ Importer :: IMPORT_TYPE_SUBSCRIBER_UPDATE , [ 'email' => $ email , 'id' => $ contactEmail -> getId ( ) ] , \ Dotdigitalgroup \ Email \ Model \ Importer :: MODE_SUBSCRIBER_UPDATE , $ websiteId ) ; } // fix for a multiple hit of the observer. stop adding the duplicates on the automation $ emailReg = $ this -> registry -> registry ( $ email . '_subscriber_save' ) ; if ( $ emailReg ) { return $ this ; } $ this -> registry -> unregister ( $ email . '_subscriber_save' ) ; // additional measure $ this -> registry -> register ( $ email . '_subscriber_save' , $ email ) ; //add subscriber to automation $ this -> addSubscriberToAutomation ( $ email , $ subscriber , $ websiteId ) ; } catch ( \ Exception $ e ) { $ this -> helper -> debug ( ( string ) $ e , [ ] ) ; } return $ this ; }
Change contact subscription status.

@param \Magento\Framework\Event\Observer $observer

@return $this
Change contact subscription status .