Skip to content

Fix newsletter subscriptions between stores #12035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions app/code/Magento/Newsletter/Model/ResourceModel/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,37 @@ public function loadByEmail($subscriberEmail)
*/
public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
{
$select = $this->connection->select()->from($this->getMainTable())->where('customer_id=:customer_id');

$result = $this->connection->fetchRow($select, ['customer_id' => $customer->getId()]);
$select = $this->connection
->select()
->from($this->getMainTable())
->where('customer_id=:customer_id and store_id=:store_id');

$result = $this->connection
->fetchRow(
$select,
[
'customer_id' => $customer->getId(),
'store_id' => $customer->getStoreId()
]
);

if ($result) {
return $result;
}

$select = $this->connection->select()->from($this->getMainTable())->where('subscriber_email=:subscriber_email');

$result = $this->connection->fetchRow($select, ['subscriber_email' => $customer->getEmail()]);
$select = $this->connection
->select()
->from($this->getMainTable())
->where('subscriber_email=:subscriber_email and store_id=:store_id');

$result = $this->connection
->fetchRow(
$select,
[
'subscriber_email' => $customer->getEmail(),
'store_id' => $customer->getStoreId()
]
);

if ($result) {
return $result;
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Newsletter/Model/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ public function loadByCustomerId($customerId)
{
try {
$customerData = $this->customerRepository->getById($customerId);
$customerData->setStoreId($this->_storeManager->getStore()->getId());
$data = $this->getResource()->loadByCustomerData($customerData);
$this->addData($data);
if (!empty($data) && $customerData->getId() && !$this->getCustomerId()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ public function testUpdateSubscription()
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');

$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
->disableOriginalConstructor()
->setMethods(['getId'])
->getMock();
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);

$this->assertEquals($this->subscriber, $this->subscriber->updateSubscription($customerId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ public function testSaveActionExistingCustomerUnsubscribeNewsletter()
'email' => '[email protected]',
'firstname' => 'test firstname',
'lastname' => 'test lastname',
'sendemail_store_id' => 1
],
'subscription' => '0'
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

$subscriber = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(\Magento\Newsletter\Model\Subscriber::class);
$subscriber->setStoreId($otherStore)
$subscriber->setStoreId($currentStore)
// Intentionally setting ID to 0 instead of 2 to test fallback mechanism in Subscriber model
->setCustomerId(0)
->setSubscriberEmail('[email protected]')
Expand Down