Skip to content

Commit 8718a60

Browse files
committed
Update newsletter module
Update module logic when customer have store_id = 0
1 parent 6f162dd commit 8718a60

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

@@ -118,6 +132,9 @@ public function loadByEmail($subscriberEmail)
118132
*/
119133
public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
120134
{
135+
$storeId = (int)$customer->getStoreId() ?: $this->storeManager
136+
->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
137+
121138
$select = $this->connection
122139
->select()
123140
->from($this->getMainTable())
@@ -128,7 +145,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
128145
$select,
129146
[
130147
'customer_id' => $customer->getId(),
131-
'store_id' => $customer->getStoreId()
148+
'store_id' => $storeId
132149
]
133150
);
134151

@@ -146,7 +163,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
146163
$select,
147164
[
148165
'subscriber_email' => $customer->getEmail(),
149-
'store_id' => $customer->getStoreId()
166+
'store_id' => $storeId
150167
]
151168
);
152169

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

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

611611
$this->setStatus($status);
612612

613+
$storeId = $customerData->getStoreId();
614+
if ((int)$customerData->getStoreId() === 0) {
615+
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
616+
}
617+
613618
if (!$this->getId()) {
614-
$storeId = $customerData->getStoreId();
615-
if ($customerData->getStoreId() == 0) {
616-
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
617-
}
618619
$this->setStoreId($storeId)
619620
->setCustomerId($customerData->getId())
620621
->setEmail($customerData->getEmail());
621622
} else {
622-
$this->setStoreId($customerData->getStoreId())
623+
$this->setStoreId($storeId)
623624
->setEmail($customerData->getEmail());
624625
}
625626

0 commit comments

Comments
 (0)