Skip to content

Commit 587b767

Browse files
author
Stanislav Idolov
authored
ENGCOM-2478: [Forwardport] Fix "Confirmation request" email is sent on customer's newsletter unsubscribe action #16995
2 parents ec27e6d + 8d34aa7 commit 587b767

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

app/code/Magento/Newsletter/Controller/Manage/Save.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
78
namespace Magento\Newsletter\Controller\Manage;
89

910
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
11+
use Magento\Newsletter\Model\Subscriber;
1012

1113
class Save extends \Magento\Newsletter\Controller\Manage
1214
{
@@ -74,13 +76,28 @@ public function execute()
7476
$customer = $this->customerRepository->getById($customerId);
7577
$storeId = $this->storeManager->getStore()->getId();
7678
$customer->setStoreId($storeId);
77-
$this->customerRepository->save($customer);
78-
if ((boolean)$this->getRequest()->getParam('is_subscribed', false)) {
79-
$this->subscriberFactory->create()->subscribeCustomerById($customerId);
80-
$this->messageManager->addSuccess(__('We saved the subscription.'));
79+
$isSubscribedState = $customer->getExtensionAttributes()
80+
->getIsSubscribed();
81+
$isSubscribedParam = (boolean)$this->getRequest()
82+
->getParam('is_subscribed', false);
83+
if ($isSubscribedParam !== $isSubscribedState) {
84+
$this->customerRepository->save($customer);
85+
if ($isSubscribedParam) {
86+
$subscribeModel = $this->subscriberFactory->create()
87+
->subscribeCustomerById($customerId);
88+
$subscribeStatus = $subscribeModel->getStatus();
89+
if ($subscribeStatus == Subscriber::STATUS_SUBSCRIBED) {
90+
$this->messageManager->addSuccess(__('We have saved your subscription.'));
91+
} else {
92+
$this->messageManager->addSuccess(__('A confirmation request has been sent.'));
93+
}
94+
} else {
95+
$this->subscriberFactory->create()
96+
->unsubscribeCustomerById($customerId);
97+
$this->messageManager->addSuccess(__('We have removed your newsletter subscription.'));
98+
}
8199
} else {
82-
$this->subscriberFactory->create()->unsubscribeCustomerById($customerId);
83-
$this->messageManager->addSuccess(__('We removed the subscription.'));
100+
$this->messageManager->addSuccess(__('We have updated your subscription.'));
84101
}
85102
} catch (\Exception $e) {
86103
$this->messageManager->addError(__('Something went wrong while saving your subscription.'));

app/code/Magento/Newsletter/Model/Subscriber.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,9 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
559559
) {
560560
$status = self::STATUS_UNCONFIRMED;
561561
} elseif ($isConfirmNeed) {
562-
$status = self::STATUS_NOT_ACTIVE;
562+
if ($this->getStatus() != self::STATUS_SUBSCRIBED) {
563+
$status = self::STATUS_NOT_ACTIVE;
564+
}
563565
}
564566
} elseif (($this->getStatus() == self::STATUS_UNCONFIRMED) && ($customerData->getConfirmation() === null)) {
565567
$status = self::STATUS_SUBSCRIBED;

dev/tests/integration/testsuite/Magento/Newsletter/Controller/ManageTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testSaveAction()
5858
* Check that success message
5959
*/
6060
$this->assertSessionMessages(
61-
$this->equalTo(['We saved the subscription.']),
61+
$this->equalTo(['We have saved your subscription.']),
6262
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
6363
);
6464
}
@@ -68,6 +68,7 @@ public function testSaveAction()
6868
*/
6969
public function testSaveActionRemoveSubscription()
7070
{
71+
7172
$this->getRequest()
7273
->setParam('form_key', 'formKey')
7374
->setParam('is_subscribed', '0');
@@ -84,7 +85,7 @@ public function testSaveActionRemoveSubscription()
8485
* Check that success message
8586
*/
8687
$this->assertSessionMessages(
87-
$this->equalTo(['We removed the subscription.']),
88+
$this->equalTo(['We have updated your subscription.']),
8889
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
8990
);
9091
}

0 commit comments

Comments
 (0)