Skip to content

Commit

Permalink
Merge forwardport of #12035 to 2.3-develop branch
Browse files Browse the repository at this point in the history
Applied pull request patch https://github.com/magento/magento2/pull/12035.patch (created by @sbaixauli) based on commit(s):
  1. 65000d8
  2. 81725aa
  3. 19fe0df
  4. 2e2f66b
  5. 194ba57

Fixed GitHub Issues in 2.3-develop branch:
  - #10014: Newsletter subscriptions status not isolated between multi stores (reported by @mikelevy300)
  • Loading branch information
magento-engcom-team committed Jan 24, 2018
2 parents 617da6c + 8b1a297 commit 39ed21f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
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' => 'customer@example.com',
'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('customer_two@example.com')
Expand Down

0 comments on commit 39ed21f

Please sign in to comment.