5
5
*/
6
6
namespace Magento \Newsletter \Model \ResourceModel ;
7
7
8
+ use Magento \Framework \App \ObjectManager ;
9
+ use Magento \Store \Model \StoreManagerInterface ;
10
+
8
11
/**
9
12
* Newsletter subscriber resource model
10
13
*
@@ -48,22 +51,30 @@ class Subscriber extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
48
51
*/
49
52
protected $ mathRandom ;
50
53
54
+ /**
55
+ * @var StoreManagerInterface
56
+ */
57
+ private $ storeManager ;
58
+
51
59
/**
52
60
* Construct
53
61
*
54
62
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
55
63
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
56
64
* @param \Magento\Framework\Math\Random $mathRandom
57
65
* @param string $connectionName
66
+ * @param StoreManagerInterface $storeManager
58
67
*/
59
68
public function __construct (
60
69
\Magento \Framework \Model \ResourceModel \Db \Context $ context ,
61
70
\Magento \Framework \Stdlib \DateTime \DateTime $ date ,
62
71
\Magento \Framework \Math \Random $ mathRandom ,
63
- $ connectionName = null
72
+ $ connectionName = null ,
73
+ StoreManagerInterface $ storeManager = null
64
74
) {
65
75
$ this ->_date = $ date ;
66
76
$ this ->mathRandom = $ mathRandom ;
77
+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (StoreManagerInterface::class);
67
78
parent ::__construct ($ context , $ connectionName );
68
79
}
69
80
@@ -117,19 +128,15 @@ public function loadByEmail($subscriberEmail)
117
128
*/
118
129
public function loadByCustomerData (\Magento \Customer \Api \Data \CustomerInterface $ customer )
119
130
{
131
+ $ storeIds = $ this ->storeManager ->getWebsite ($ customer ->getWebsiteId ())->getStoreIds ();
132
+
120
133
$ select = $ this ->connection
121
134
->select ()
122
135
->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 );
133
140
134
141
if ($ result ) {
135
142
return $ result ;
@@ -138,16 +145,10 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
138
145
$ select = $ this ->connection
139
146
->select ()
140
147
->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 );
151
152
152
153
if ($ result ) {
153
154
return $ result ;
0 commit comments