Skip to content

Commit 7c5962a

Browse files
committed
MAGETWO-91684: Issue with Newsletter subscriptions
- Changing subscription behavior to websites
1 parent 2eb6f84 commit 7c5962a

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Newsletter\Model\ResourceModel;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Store\Model\StoreManagerInterface;
10+
811
/**
912
* Newsletter subscriber resource model
1013
*
@@ -48,22 +51,30 @@ class Subscriber extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
4851
*/
4952
protected $mathRandom;
5053

54+
/**
55+
* @var StoreManagerInterface
56+
*/
57+
private $storeManager;
58+
5159
/**
5260
* Construct
5361
*
5462
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
5563
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
5664
* @param \Magento\Framework\Math\Random $mathRandom
5765
* @param string $connectionName
66+
* @param StoreManagerInterface $storeManager
5867
*/
5968
public function __construct(
6069
\Magento\Framework\Model\ResourceModel\Db\Context $context,
6170
\Magento\Framework\Stdlib\DateTime\DateTime $date,
6271
\Magento\Framework\Math\Random $mathRandom,
63-
$connectionName = null
72+
$connectionName = null,
73+
StoreManagerInterface $storeManager = null
6474
) {
6575
$this->_date = $date;
6676
$this->mathRandom = $mathRandom;
77+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
6778
parent::__construct($context, $connectionName);
6879
}
6980

@@ -117,19 +128,15 @@ public function loadByEmail($subscriberEmail)
117128
*/
118129
public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
119130
{
131+
$storeIds = $this->storeManager->getWebsite($customer->getWebsiteId())->getStoreIds();
132+
120133
$select = $this->connection
121134
->select()
122135
->from($this->getMainTable())
123-
->where('customer_id=:customer_id and store_id=:store_id');
124-
125-
$result = $this->connection
126-
->fetchRow(
127-
$select,
128-
[
129-
'customer_id' => $customer->getId(),
130-
'store_id' => $customer->getStoreId()
131-
]
132-
);
136+
->where('customer_id = ?', $customer->getId())
137+
->where('store_id IN (?)', $storeIds);
138+
139+
$result = $this->connection->fetchRow($select);
133140

134141
if ($result) {
135142
return $result;
@@ -138,16 +145,10 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
138145
$select = $this->connection
139146
->select()
140147
->from($this->getMainTable())
141-
->where('subscriber_email=:subscriber_email and store_id=:store_id');
142-
143-
$result = $this->connection
144-
->fetchRow(
145-
$select,
146-
[
147-
'subscriber_email' => $customer->getEmail(),
148-
'store_id' => $customer->getStoreId()
149-
]
150-
);
148+
->where('subscriber_email = ?', $customer->getEmail())
149+
->where('store_id IN (?)', $storeIds);
150+
151+
$result = $this->connection->fetchRow($select);
151152

152153
if ($result) {
153154
return $result;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ public function loadByCustomerId($customerId)
392392
try {
393393
$customerData = $this->customerRepository->getById($customerId);
394394
$customerData->setStoreId($this->_storeManager->getStore()->getId());
395+
if ($customerData->getWebsiteId() === null) {
396+
$customerData->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());
397+
}
395398
$data = $this->getResource()->loadByCustomerData($customerData);
396399
$this->addData($data);
397400
if (!empty($data) && $customerData->getId() && !$this->getCustomerId()) {

0 commit comments

Comments
 (0)