Save.php 4.8 KB

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