diff --git a/app/code/Magento/Newsletter/Controller/Manage/Save.php b/app/code/Magento/Newsletter/Controller/Manage/Save.php index 75ef8b26f50a9..419cbac10ffd1 100644 --- a/app/code/Magento/Newsletter/Controller/Manage/Save.php +++ b/app/code/Magento/Newsletter/Controller/Manage/Save.php @@ -4,9 +4,11 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Newsletter\Controller\Manage; use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository; +use Magento\Newsletter\Model\Subscriber; class Save extends \Magento\Newsletter\Controller\Manage { @@ -74,13 +76,28 @@ public function execute() $customer = $this->customerRepository->getById($customerId); $storeId = $this->storeManager->getStore()->getId(); $customer->setStoreId($storeId); - $this->customerRepository->save($customer); - if ((boolean)$this->getRequest()->getParam('is_subscribed', false)) { - $this->subscriberFactory->create()->subscribeCustomerById($customerId); - $this->messageManager->addSuccess(__('We saved the subscription.')); + $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->subscriberFactory->create()->unsubscribeCustomerById($customerId); - $this->messageManager->addSuccess(__('We removed the subscription.')); + $this->messageManager->addSuccess(__('We have updated your subscription.')); } } catch (\Exception $e) { $this->messageManager->addError(__('Something went wrong while saving your subscription.')); diff --git a/app/code/Magento/Newsletter/Model/Subscriber.php b/app/code/Magento/Newsletter/Model/Subscriber.php index 76d55de5954b1..27ee8197778ff 100644 --- a/app/code/Magento/Newsletter/Model/Subscriber.php +++ b/app/code/Magento/Newsletter/Model/Subscriber.php @@ -559,7 +559,9 @@ protected function _updateCustomerSubscription($customerId, $subscribe) ) { $status = self::STATUS_UNCONFIRMED; } elseif ($isConfirmNeed) { - $status = self::STATUS_NOT_ACTIVE; + if ($this->getStatus() != self::STATUS_SUBSCRIBED) { + $status = self::STATUS_NOT_ACTIVE; + } } } elseif (($this->getStatus() == self::STATUS_UNCONFIRMED) && ($customerData->getConfirmation() === null)) { $status = self::STATUS_SUBSCRIBED; diff --git a/dev/tests/integration/testsuite/Magento/Newsletter/Controller/ManageTest.php b/dev/tests/integration/testsuite/Magento/Newsletter/Controller/ManageTest.php index 1a27994b607f1..90892be1327c9 100644 --- a/dev/tests/integration/testsuite/Magento/Newsletter/Controller/ManageTest.php +++ b/dev/tests/integration/testsuite/Magento/Newsletter/Controller/ManageTest.php @@ -58,7 +58,7 @@ public function testSaveAction() * Check that success message */ $this->assertSessionMessages( - $this->equalTo(['We saved the subscription.']), + $this->equalTo(['We have saved your subscription.']), \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS ); } @@ -68,6 +68,7 @@ public function testSaveAction() */ public function testSaveActionRemoveSubscription() { + $this->getRequest() ->setParam('form_key', 'formKey') ->setParam('is_subscribed', '0'); @@ -84,7 +85,7 @@ public function testSaveActionRemoveSubscription() * Check that success message */ $this->assertSessionMessages( - $this->equalTo(['We removed the subscription.']), + $this->equalTo(['We have updated your subscription.']), \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS ); }