1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* 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\Customer\Model\Customer;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Newsletter\Model\Subscriber;
/**
* Customers newsletter subscription save controller
*/
class Save extends \Magento\Newsletter\Controller\Manage implements HttpPostActionInterface, HttpGetActionInterface
{
/**
* @var \Magento\Framework\Data\Form\FormKey\Validator
*/
protected $formKeyValidator;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var CustomerRepository
*/
protected $customerRepository;
/**
* @var \Magento\Newsletter\Model\SubscriberFactory
*/
protected $subscriberFactory;
/**
* Initialize dependencies.
*
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param CustomerRepository $customerRepository
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
\Magento\Store\Model\StoreManagerInterface $storeManager,
CustomerRepository $customerRepository,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
) {
$this->storeManager = $storeManager;
$this->formKeyValidator = $formKeyValidator;
$this->customerRepository = $customerRepository;
$this->subscriberFactory = $subscriberFactory;
parent::__construct($context, $customerSession);
}
/**
* Save newsletter subscription preference action
*
* @return void|null
*/
public function execute()
{
if (!$this->formKeyValidator->validate($this->getRequest())) {
return $this->_redirect('customer/account/');
}
$customerId = $this->_customerSession->getCustomerId();
if ($customerId === null) {
$this->messageManager->addError(__('Something went wrong while saving your subscription.'));
} else {
try {
$customer = $this->customerRepository->getById($customerId);
$storeId = $this->storeManager->getStore()->getId();
$customer->setStoreId($storeId);
$isSubscribedState = $customer->getExtensionAttributes()
->getIsSubscribed();
$isSubscribedParam = (boolean)$this->getRequest()
->getParam('is_subscribed', false);
if ($isSubscribedParam !== $isSubscribedState) {
// No need to validate customer and customer address while saving subscription preferences
$this->setIgnoreValidationFlag($customer);
$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->messageManager->addSuccess(__('We have updated your subscription.'));
}
} catch (\Exception $e) {
$this->messageManager->addError(__('Something went wrong while saving your subscription.'));
}
}
$this->_redirect('customer/account/');
}
/**
* Set ignore_validation_flag to skip unnecessary address and customer validation
*
* @param Customer $customer
* @return void
*/
private function setIgnoreValidationFlag($customer)
{
$customer->setData('ignore_validation_flag', true);
}
}