Skip to content

Commit

Permalink
Fix "Confirmation request" email is sent on customer's newsletter uns…
Browse files Browse the repository at this point in the history
…ubscription (issues/15218). Skip update customer subscribe status from save subscribe action from my account when nothing is changed.
  • Loading branch information
anvasiliev committed May 18, 2018
1 parent 48d9e3f commit d790198
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
30 changes: 24 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,29 @@ 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 saved the subscription.'));
} else {
$this->messageManager->addSuccess(__('The confirmation request has been sent.'));
}
} else {
$this->subscriberFactory->create()
->unsubscribeCustomerById($customerId);
$this->messageManager->addSuccess(__('We removed the subscription.'));
}
} else {
$this->subscriberFactory->create()->unsubscribeCustomerById($customerId);
$this->messageManager->addSuccess(__('We removed the subscription.'));
$this->_redirect('newsletter/manage/');
return;
}
} 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

0 comments on commit d790198

Please sign in to comment.