Skip to content

[Forwardport] Fix "Confirmation request" email is sent on customer's newsletter unsubscribe action #16995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions app/code/Magento/Newsletter/Controller/Manage/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.'));
Expand Down
4 changes: 3 additions & 1 deletion app/code/Magento/Newsletter/Model/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand All @@ -68,6 +68,7 @@ public function testSaveAction()
*/
public function testSaveActionRemoveSubscription()
{

$this->getRequest()
->setParam('form_key', 'formKey')
->setParam('is_subscribed', '0');
Expand All @@ -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
);
}
Expand Down