|
4 | 4 | * Copyright © Magento, Inc. All rights reserved.
|
5 | 5 | * See COPYING.txt for license details.
|
6 | 6 | */
|
| 7 | + |
7 | 8 | namespace Magento\Newsletter\Controller\Manage;
|
8 | 9 |
|
9 | 10 | use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
|
| 11 | +use Magento\Newsletter\Model\Subscriber; |
10 | 12 |
|
11 | 13 | class Save extends \Magento\Newsletter\Controller\Manage
|
12 | 14 | {
|
@@ -74,13 +76,29 @@ public function execute()
|
74 | 76 | $customer = $this->customerRepository->getById($customerId);
|
75 | 77 | $storeId = $this->storeManager->getStore()->getId();
|
76 | 78 | $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 | + } |
81 | 99 | } else {
|
82 |
| - $this->subscriberFactory->create()->unsubscribeCustomerById($customerId); |
83 |
| - $this->messageManager->addSuccess(__('We removed the subscription.')); |
| 100 | + $this->_redirect('newsletter/manage/'); |
| 101 | + return; |
84 | 102 | }
|
85 | 103 | } catch (\Exception $e) {
|
86 | 104 | $this->messageManager->addError(__('Something went wrong while saving your subscription.'));
|
|
0 commit comments