|
5 | 5 | */
|
6 | 6 | namespace Magento\Newsletter\Model\ResourceModel;
|
7 | 7 |
|
8 |
| -use Magento\Store\Model\StoreManagerInterface; |
9 | 8 | use Magento\Framework\App\ObjectManager;
|
| 9 | +use Magento\Store\Model\StoreManagerInterface; |
10 | 10 |
|
11 | 11 | /**
|
12 | 12 | * Newsletter subscriber resource model
|
@@ -76,8 +76,7 @@ public function __construct(
|
76 | 76 | ) {
|
77 | 77 | $this->_date = $date;
|
78 | 78 | $this->mathRandom = $mathRandom;
|
79 |
| - $this->storeManager = $storeManager ?: ObjectManager::getInstance() |
80 |
| - ->get(StoreManagerInterface::class); |
| 79 | + $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class); |
81 | 80 | parent::__construct($context, $connectionName);
|
82 | 81 | }
|
83 | 82 |
|
@@ -131,43 +130,36 @@ public function loadByEmail($subscriberEmail)
|
131 | 130 | */
|
132 | 131 | public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
|
133 | 132 | {
|
134 |
| - $storeId = (int)$customer->getStoreId() ?: $this->storeManager |
135 |
| - ->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId(); |
136 |
| - |
137 |
| - $select = $this->connection |
138 |
| - ->select() |
139 |
| - ->from($this->getMainTable()) |
140 |
| - ->where('customer_id=:customer_id and store_id=:store_id'); |
141 |
| - |
142 |
| - $result = $this->connection |
143 |
| - ->fetchRow( |
144 |
| - $select, |
145 |
| - [ |
146 |
| - 'customer_id' => $customer->getId(), |
147 |
| - 'store_id' => $storeId |
148 |
| - ] |
149 |
| - ); |
| 133 | + $storeIds = $this->storeManager->getWebsite($customer->getWebsiteId())->getStoreIds(); |
| 134 | + |
| 135 | + if ($customer->getId()) { |
| 136 | + $select = $this->connection |
| 137 | + ->select() |
| 138 | + ->from($this->getMainTable()) |
| 139 | + ->where('customer_id = ?', $customer->getId()) |
| 140 | + ->where('store_id IN (?)', $storeIds) |
| 141 | + ->limit(1); |
150 | 142 |
|
151 |
| - if ($result) { |
152 |
| - return $result; |
| 143 | + $result = $this->connection->fetchRow($select); |
| 144 | + |
| 145 | + if ($result) { |
| 146 | + return $result; |
| 147 | + } |
153 | 148 | }
|
154 | 149 |
|
155 |
| - $select = $this->connection |
156 |
| - ->select() |
157 |
| - ->from($this->getMainTable()) |
158 |
| - ->where('subscriber_email=:subscriber_email and store_id=:store_id'); |
159 |
| - |
160 |
| - $result = $this->connection |
161 |
| - ->fetchRow( |
162 |
| - $select, |
163 |
| - [ |
164 |
| - 'subscriber_email' => $customer->getEmail(), |
165 |
| - 'store_id' => $storeId |
166 |
| - ] |
167 |
| - ); |
| 150 | + if ($customer->getEmail()) { |
| 151 | + $select = $this->connection |
| 152 | + ->select() |
| 153 | + ->from($this->getMainTable()) |
| 154 | + ->where('subscriber_email = ?', $customer->getEmail()) |
| 155 | + ->where('store_id IN (?)', $storeIds) |
| 156 | + ->limit(1); |
| 157 | + |
| 158 | + $result = $this->connection->fetchRow($select); |
168 | 159 |
|
169 |
| - if ($result) { |
170 |
| - return $result; |
| 160 | + if ($result) { |
| 161 | + return $result; |
| 162 | + } |
171 | 163 | }
|
172 | 164 |
|
173 | 165 | return [];
|
|
0 commit comments