11
11
use Magento \Framework \Exception \MailException ;
12
12
use Magento \Framework \Exception \NoSuchEntityException ;
13
13
use Magento \Framework \Stdlib \DateTime \DateTime ;
14
+ use Magento \Customer \Api \Data \CustomerInterfaceFactory ;
15
+ use Magento \Framework \Api \DataObjectHelper ;
14
16
15
17
/**
16
18
* Subscriber model
@@ -129,6 +131,16 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
129
131
*/
130
132
protected $ inlineTranslation ;
131
133
134
+ /**
135
+ * @var CustomerInterfaceFactory
136
+ */
137
+ private $ customerFactory ;
138
+
139
+ /**
140
+ * @var DataObjectHelper
141
+ */
142
+ private $ dataObjectHelper ;
143
+
132
144
/**
133
145
* Initialize dependencies.
134
146
*
@@ -146,6 +158,8 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
146
158
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
147
159
* @param array $data
148
160
* @param DateTime|null $dateTime
161
+ * @param CustomerInterfaceFactory|null $customerFactory
162
+ * @param DataObjectHelper|null $dataObjectHelper
149
163
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
150
164
*/
151
165
public function __construct (
@@ -162,7 +176,9 @@ public function __construct(
162
176
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
163
177
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
164
178
array $ data = [],
165
- DateTime $ dateTime = null
179
+ DateTime $ dateTime = null ,
180
+ CustomerInterfaceFactory $ customerFactory = null ,
181
+ DataObjectHelper $ dataObjectHelper = null
166
182
) {
167
183
$ this ->_newsletterData = $ newsletterData ;
168
184
$ this ->_scopeConfig = $ scopeConfig ;
@@ -173,6 +189,8 @@ public function __construct(
173
189
$ this ->customerAccountManagement = $ customerAccountManagement ;
174
190
$ this ->inlineTranslation = $ inlineTranslation ;
175
191
$ this ->dateTime = $ dateTime ?: ObjectManager::getInstance ()->get (DateTime::class);
192
+ $ this ->customerFactory = $ customerFactory ?: ObjectManager::getInstance ()->get (CustomerInterfaceFactory::class);
193
+ $ this ->dataObjectHelper = $ dataObjectHelper ?: ObjectManager::getInstance ()->get (DataObjectHelper::class);
176
194
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
177
195
}
178
196
@@ -405,7 +423,17 @@ public function randomSequence($length = 32)
405
423
*/
406
424
public function subscribe ($ email )
407
425
{
408
- $ this ->loadByEmail ($ email );
426
+ $ storeId = $ this ->_storeManager ->getStore ()->getId ();
427
+ $ customerData = ['store_id ' => $ storeId , 'email ' => $ email ];
428
+
429
+ /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
430
+ $ customer = $ this ->customerFactory ->create ();
431
+ $ this ->dataObjectHelper ->populateWithArray (
432
+ $ customer ,
433
+ $ customerData ,
434
+ \Magento \Customer \Api \Data \CustomerInterface::class
435
+ );
436
+ $ this ->getResource ()->loadByCustomerData ($ customer );
409
437
410
438
if ($ this ->getId () && $ this ->getStatus () == self ::STATUS_SUBSCRIBED ) {
411
439
return $ this ->getStatus ();
0 commit comments