Skip to content

Fix "Confirmation request" email is sent on customer's newsletter unsubscribe action #15464

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 5 commits into from
Jul 3, 2018
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole section is still a bit hard to read/understand do you think that there would be a way to make it a bit easier to read? Maybe moving some of the logic to new private methods and splitting out some of the if/else so it is not so nested. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dmanners
Thats a reasonable proposition, cause I agree that this part of newsletter module is not the best, but for now I will leave it as it is. If I will find enough time I will come back to it later.

->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 @@ -568,7 +568,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