Skip to content

Commit 2e1d92f

Browse files
committed
Update newsletter module
Update module logic when customer have store_id = 0
1 parent 80469a6 commit 2e1d92f

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

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

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

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

54+
/**
55+
* Store manager
56+
*
57+
* @var StoreManagerInterface
58+
*/
59+
private $storeManager;
60+
5161
/**
5262
* Construct
5363
*
5464
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
5565
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
5666
* @param \Magento\Framework\Math\Random $mathRandom
5767
* @param string $connectionName
68+
* @param StoreManagerInterface $storeManager
5869
*/
5970
public function __construct(
6071
\Magento\Framework\Model\ResourceModel\Db\Context $context,
6172
\Magento\Framework\Stdlib\DateTime\DateTime $date,
6273
\Magento\Framework\Math\Random $mathRandom,
63-
$connectionName = null
74+
$connectionName = null,
75+
StoreManagerInterface $storeManager = null
6476
) {
6577
$this->_date = $date;
6678
$this->mathRandom = $mathRandom;
79+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
80+
->get(StoreManagerInterface::class);
6781
parent::__construct($context, $connectionName);
6882
}
6983

@@ -117,6 +131,9 @@ public function loadByEmail($subscriberEmail)
117131
*/
118132
public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
119133
{
134+
$storeId = (int)$customer->getStoreId() ?: $this->storeManager
135+
->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
136+
120137
$select = $this->connection
121138
->select()
122139
->from($this->getMainTable())
@@ -127,7 +144,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
127144
$select,
128145
[
129146
'customer_id' => $customer->getId(),
130-
'store_id' => $customer->getStoreId()
147+
'store_id' => $storeId
131148
]
132149
);
133150

@@ -145,7 +162,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
145162
$select,
146163
[
147164
'subscriber_email' => $customer->getEmail(),
148-
'store_id' => $customer->getStoreId()
165+
'store_id' => $storeId
149166
]
150167
);
151168

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,16 +613,17 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
613613

614614
$this->setStatus($status);
615615

616+
$storeId = $customerData->getStoreId();
617+
if ((int)$customerData->getStoreId() === 0) {
618+
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
619+
}
620+
616621
if (!$this->getId()) {
617-
$storeId = $customerData->getStoreId();
618-
if ($customerData->getStoreId() == 0) {
619-
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
620-
}
621622
$this->setStoreId($storeId)
622623
->setCustomerId($customerData->getId())
623624
->setEmail($customerData->getEmail());
624625
} else {
625-
$this->setStoreId($customerData->getStoreId())
626+
$this->setStoreId($storeId)
626627
->setEmail($customerData->getEmail());
627628
}
628629

0 commit comments

Comments
 (0)