Skip to content

Commit d790198

Browse files
committed
Fix "Confirmation request" email is sent on customer's newsletter unsubscription (issues/15218). Skip update customer subscribe status from save subscribe action from my account when nothing is changed.
1 parent 48d9e3f commit d790198

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

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

Lines changed: 24 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,29 @@ 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 saved the subscription.'));
91+
} else {
92+
$this->messageManager->addSuccess(__('The confirmation request has been sent.'));
93+
}
94+
} else {
95+
$this->subscriberFactory->create()
96+
->unsubscribeCustomerById($customerId);
97+
$this->messageManager->addSuccess(__('We removed the subscription.'));
98+
}
8199
} else {
82-
$this->subscriberFactory->create()->unsubscribeCustomerById($customerId);
83-
$this->messageManager->addSuccess(__('We removed the subscription.'));
100+
$this->_redirect('newsletter/manage/');
101+
return;
84102
}
85103
} catch (\Exception $e) {
86104
$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
@@ -568,7 +568,9 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
568568
) {
569569
$status = self::STATUS_UNCONFIRMED;
570570
} elseif ($isConfirmNeed) {
571-
$status = self::STATUS_NOT_ACTIVE;
571+
if ($this->getStatus() != self::STATUS_SUBSCRIBED) {
572+
$status = self::STATUS_NOT_ACTIVE;
573+
}
572574
}
573575
} elseif (($this->getStatus() == self::STATUS_UNCONFIRMED) && ($customerData->getConfirmation() === null)) {
574576
$status = self::STATUS_SUBSCRIBED;

0 commit comments

Comments
 (0)