Controller/Customer/Newsletter.php
private function processGeneralSubscription()
    {
        $customerId = $this->customerSession->getCustomerId();
        if ($customerId === null) {
            $this->messageManager->addError(__('Something went wrong while saving your subscription.'));
        } else {
            try {
                $customer = $this->customerRepository->getById($customerId);
                $storeId = $this->helper->storeManager->getStore()->getId();
                $customer->setStoreId($storeId);
                $isSubscribedState = $customer->getExtensionAttributes()
                    ->getIsSubscribed();
                $isSubscribedParam = (boolean)$this->getRequest()
                    ->getParam('is_subscribed', false);
                if ($isSubscribedParam !== $isSubscribedState) {
                    $this->customerRepository->save($customer);
                    if ($isSubscribedParam) {
                        $subscribeModel = $this->subscriberFactory->create()
                            ->subscribeCustomerById($customerId);
                        $subscribeStatus = $subscribeModel->getStatus();
                        if ($subscribeStatus == Subscriber::STATUS_SUBSCRIBED) {
                            $this->messageManager->addSuccess(__('We have saved your subscription.'));
                        } else {
                            $this->messageManager->addSuccess(__('A confirmation request has been sent.'));
                        }
                    } else {
                        $this->subscriberFactory->create()
                            ->unsubscribeCustomerById($customerId);
                        $this->messageManager->addSuccess(__('We have removed your newsletter subscription.'));
                    }
                } else {
                    $this->messageManager->addSuccess(__('We have updated your subscription.'));
                }
            } catch (\Exception $e) {
                $this->messageManager->addError(__('Something went wrong while saving your subscription.'));
            }
        }
    }
private function processGeneralSubscription()
    {
        $customerId = $this->customerSession->getCustomerId();
        if ($customerId === null) {
            $this->messageManager->addError(__('Something went wrong while saving your subscription.'));
        } else {
            try {
                $customer = $this->customerRepository->getById($customerId);
                $storeId = $this->helper->storeManager->getStore()->getId();
                $customer->setStoreId($storeId);
                $isSubscribedState = $customer->getExtensionAttributes()
                    ->getIsSubscribed();
                $isSubscribedParam = (boolean)$this->getRequest()
                    ->getParam('is_subscribed', false);
                if ($isSubscribedParam !== $isSubscribedState) {
                    $this->customerRepository->save($customer);
                    if ($isSubscribedParam) {
                        $subscribeModel = $this->subscriberFactory->create()
                            ->subscribeCustomerById($customerId);
                        $subscribeStatus = $subscribeModel->getStatus();
                        if ($subscribeStatus == Subscriber::STATUS_SUBSCRIBED) {
                            $this->messageManager->addSuccess(__('We have saved your subscription.'));
                        } else {
                            $this->messageManager->addSuccess(__('A confirmation request has been sent.'));
                        }
                    } else {
                        $this->subscriberFactory->create()
                            ->unsubscribeCustomerById($customerId);
                        $this->messageManager->addSuccess(__('We have removed your newsletter subscription.'));
                    }
                } else {
                    $this->messageManager->addSuccess(__('We have updated your subscription.'));
                }
            } catch (\Exception $e) {
                $this->messageManager->addError(__('Something went wrong while saving your subscription.'));
            }
        }
    }
private function processGeneralSubscription ( ) { $ customerId = $ this -> customerSession -> getCustomerId ( ) ; if ( $ customerId === null ) { $ this -> messageManager -> addError ( __ ( 'Something went wrong while saving your subscription.' ) ) ; } else { try { $ customer = $ this -> customerRepository -> getById ( $ customerId ) ; $ storeId = $ this -> helper -> storeManager -> getStore ( ) -> getId ( ) ; $ customer -> setStoreId ( $ storeId ) ; $ isSubscribedState = $ customer -> getExtensionAttributes ( ) -> getIsSubscribed ( ) ; $ isSubscribedParam = ( boolean ) $ this -> getRequest ( ) -> getParam ( 'is_subscribed' , false ) ; if ( $ isSubscribedParam !== $ isSubscribedState ) { $ this -> customerRepository -> save ( $ customer ) ; if ( $ isSubscribedParam ) { $ subscribeModel = $ this -> subscriberFactory -> create ( ) -> subscribeCustomerById ( $ customerId ) ; $ subscribeStatus = $ subscribeModel -> getStatus ( ) ; if ( $ subscribeStatus == Subscriber :: STATUS_SUBSCRIBED ) { $ this -> messageManager -> addSuccess ( __ ( 'We have saved your subscription.' ) ) ; } else { $ this -> messageManager -> addSuccess ( __ ( 'A confirmation request has been sent.' ) ) ; } } else { $ this -> subscriberFactory -> create ( ) -> unsubscribeCustomerById ( $ customerId ) ; $ this -> messageManager -> addSuccess ( __ ( 'We have removed your newsletter subscription.' ) ) ; } } else { $ this -> messageManager -> addSuccess ( __ ( 'We have updated your subscription.' ) ) ; } } catch ( \ Exception $ e ) { $ this -> messageManager -> addError ( __ ( 'Something went wrong while saving your subscription.' ) ) ; } } }
Process general subscription
Process general subscription