Skip to content

Commit ef2f317

Browse files
author
olysenko
committed
MAGETWO-88217: Guest users unable to sign up to newsletters between stores
1 parent 1ff3ca5 commit ef2f317

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

app/code/Magento/Newsletter/Controller/Subscriber/NewAction.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ public function execute()
125125
$this->validateEmailAvailable($email);
126126

127127
$subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
128+
$storeId = $this->_storeManager->getStore()->getId();
128129
if ($subscriber->getId()
129130
&& $subscriber->getSubscriberStatus() == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
131+
&& $subscriber->getStoreId() === $storeId
130132
) {
131133
throw new \Magento\Framework\Exception\LocalizedException(
132134
__('This email address is already subscribed.')

app/code/Magento/Newsletter/Model/Subscriber.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Framework\Exception\MailException;
1212
use Magento\Framework\Exception\NoSuchEntityException;
1313
use Magento\Framework\Stdlib\DateTime\DateTime;
14+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
15+
use Magento\Framework\Api\DataObjectHelper;
1416

1517
/**
1618
* Subscriber model
@@ -129,6 +131,16 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
129131
*/
130132
protected $inlineTranslation;
131133

134+
/**
135+
* @var CustomerInterfaceFactory
136+
*/
137+
private $customerFactory;
138+
139+
/**
140+
* @var DataObjectHelper
141+
*/
142+
private $dataObjectHelper;
143+
132144
/**
133145
* Initialize dependencies.
134146
*
@@ -146,6 +158,8 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
146158
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
147159
* @param array $data
148160
* @param DateTime|null $dateTime
161+
* @param CustomerInterfaceFactory|null $customerFactory
162+
* @param DataObjectHelper|null $dataObjectHelper
149163
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
150164
*/
151165
public function __construct(
@@ -162,7 +176,9 @@ public function __construct(
162176
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
163177
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
164178
array $data = [],
165-
DateTime $dateTime = null
179+
DateTime $dateTime = null,
180+
CustomerInterfaceFactory $customerFactory = null,
181+
DataObjectHelper $dataObjectHelper = null
166182
) {
167183
$this->_newsletterData = $newsletterData;
168184
$this->_scopeConfig = $scopeConfig;
@@ -173,6 +189,8 @@ public function __construct(
173189
$this->customerAccountManagement = $customerAccountManagement;
174190
$this->inlineTranslation = $inlineTranslation;
175191
$this->dateTime = $dateTime ?: ObjectManager::getInstance()->get(DateTime::class);
192+
$this->customerFactory = $customerFactory ?: ObjectManager::getInstance()->get(CustomerInterfaceFactory::class);
193+
$this->dataObjectHelper = $dataObjectHelper ?: ObjectManager::getInstance()->get(DataObjectHelper::class);
176194
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
177195
}
178196

@@ -405,7 +423,17 @@ public function randomSequence($length = 32)
405423
*/
406424
public function subscribe($email)
407425
{
408-
$this->loadByEmail($email);
426+
$storeId = $this->_storeManager->getStore()->getId();
427+
$customerData = ['store_id' => $storeId, 'email'=> $email];
428+
429+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
430+
$customer = $this->customerFactory->create();
431+
$this->dataObjectHelper->populateWithArray(
432+
$customer,
433+
$customerData,
434+
\Magento\Customer\Api\Data\CustomerInterface::class
435+
);
436+
$this->getResource()->loadByCustomerData($customer);
409437

410438
if ($this->getId() && $this->getStatus() == self::STATUS_SUBSCRIBED) {
411439
return $this->getStatus();

0 commit comments

Comments
 (0)