7
7
8
8
use Magento \Customer \Api \AccountManagementInterface ;
9
9
use Magento \Customer \Api \CustomerRepositoryInterface ;
10
- use Magento \Framework \Exception \ NoSuchEntityException ;
10
+ use Magento \Framework \App \ ObjectManager ;
11
11
use Magento \Framework \Exception \MailException ;
12
+ use Magento \Framework \Exception \NoSuchEntityException ;
13
+ use Magento \Framework \Stdlib \DateTime \DateTime ;
12
14
13
15
/**
14
16
* Subscriber model
15
17
*
16
- * @method \Magento\Newsletter\Model\ResourceModel\Subscriber _getResource()
17
- * @method \Magento\Newsletter\Model\ResourceModel\Subscriber getResource()
18
18
* @method int getStoreId()
19
19
* @method $this setStoreId(int $value)
20
20
* @method string getChangeStatusAt()
@@ -93,6 +93,13 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
93
93
*/
94
94
protected $ _customerSession ;
95
95
96
+ /**
97
+ * Date
98
+ *
99
+ * @var DateTime
100
+ */
101
+ private $ dateTime ;
102
+
96
103
/**
97
104
* Store manager
98
105
*
@@ -133,9 +140,10 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
133
140
* @param CustomerRepositoryInterface $customerRepository
134
141
* @param AccountManagementInterface $customerAccountManagement
135
142
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
136
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
137
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
143
+ * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
144
+ * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
138
145
* @param array $data
146
+ * @param DateTime|null $dateTime
139
147
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
140
148
*/
141
149
public function __construct (
@@ -151,7 +159,8 @@ public function __construct(
151
159
\Magento \Framework \Translate \Inline \StateInterface $ inlineTranslation ,
152
160
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
153
161
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
154
- array $ data = []
162
+ array $ data = [],
163
+ DateTime $ dateTime = null
155
164
) {
156
165
$ this ->_newsletterData = $ newsletterData ;
157
166
$ this ->_scopeConfig = $ scopeConfig ;
@@ -161,6 +170,7 @@ public function __construct(
161
170
$ this ->customerRepository = $ customerRepository ;
162
171
$ this ->customerAccountManagement = $ customerAccountManagement ;
163
172
$ this ->inlineTranslation = $ inlineTranslation ;
173
+ $ this ->dateTime = $ dateTime ?: ObjectManager::getInstance ()->get (DateTime::class);
164
174
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
165
175
}
166
176
@@ -263,7 +273,6 @@ public function setStatus($value)
263
273
* @param boolean $scope
264
274
* @return $this
265
275
*/
266
-
267
276
public function setMessagesScope ($ scope )
268
277
{
269
278
$ this ->getResource ()->setMessagesScope ($ scope );
@@ -349,6 +358,7 @@ public function loadByCustomerId($customerId)
349
358
{
350
359
try {
351
360
$ customerData = $ this ->customerRepository ->getById ($ customerId );
361
+ $ customerData ->setStoreId ($ this ->_storeManager ->getStore ()->getId ());
352
362
$ data = $ this ->getResource ()->loadByCustomerData ($ customerData );
353
363
$ this ->addData ($ data );
354
364
if (!empty ($ data ) && $ customerData ->getId () && !$ this ->getCustomerId ()) {
@@ -446,6 +456,7 @@ public function subscribe($email)
446
456
$ this ->setStatusChanged (true );
447
457
448
458
try {
459
+ /* Save model before sending out email */
449
460
$ this ->save ();
450
461
if ($ isConfirmNeed === true
451
462
&& $ isOwnSubscribes === false
@@ -544,15 +555,13 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
544
555
}
545
556
546
557
$ sendInformationEmail = false ;
547
- $ status = self ::STATUS_SUBSCRIBED ;
548
- if ($ subscribe ) {
549
- if (AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED
550
- == $ this ->customerAccountManagement ->getConfirmationStatus ($ customerId )
551
- ) {
552
- $ status = self ::STATUS_UNCONFIRMED ;
553
- }
554
- } else {
555
- $ status = self ::STATUS_UNSUBSCRIBED ;
558
+ $ isConfirmNeed = 1 === (int )$ this ->_scopeConfig ->getValue (
559
+ self ::XML_PATH_CONFIRMATION_FLAG ,
560
+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
561
+ );
562
+ $ status = $ this ->getSubscriptionStatus ($ subscribe , $ isConfirmNeed , $ customerId );
563
+ if ($ status == self ::STATUS_SUBSCRIBED && $ customerData ->getConfirmation () === null ) {
564
+ $ sendInformationEmail = true ;
556
565
}
557
566
/**
558
567
* If subscription status has been changed then send email to the customer
@@ -584,7 +593,9 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
584
593
$ sendSubscription = $ sendInformationEmail ;
585
594
if ($ sendSubscription === null xor $ sendSubscription ) {
586
595
try {
587
- if ($ this ->isStatusChanged () && $ status == self ::STATUS_UNSUBSCRIBED ) {
596
+ if ($ isConfirmNeed ) {
597
+ $ this ->sendConfirmationRequestEmail ();
598
+ } elseif ($ this ->isStatusChanged () && $ status == self ::STATUS_UNSUBSCRIBED ) {
588
599
$ this ->sendUnsubscriptionEmail ();
589
600
} elseif ($ this ->isStatusChanged () && $ status == self ::STATUS_SUBSCRIBED ) {
590
601
$ this ->sendConfirmationSuccessEmail ();
@@ -597,6 +608,32 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
597
608
return $ this ;
598
609
}
599
610
611
+ /**
612
+ * Get subscription status
613
+ *
614
+ * @param bool $subscribe
615
+ * @param bool $isConfirmNeed
616
+ * @param string $customerId
617
+ * @return int
618
+ */
619
+ private function getSubscriptionStatus ($ subscribe , $ isConfirmNeed , $ customerId )
620
+ {
621
+ $ status = self ::STATUS_SUBSCRIBED ;
622
+ if ($ subscribe ) {
623
+ if (AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED
624
+ == $ this ->customerAccountManagement ->getConfirmationStatus ($ customerId )
625
+ ) {
626
+ $ status = self ::STATUS_UNCONFIRMED ;
627
+ } elseif ($ isConfirmNeed ) {
628
+ $ status = self ::STATUS_NOT_ACTIVE ;
629
+ }
630
+ } elseif (($ this ->getStatus () != self ::STATUS_UNCONFIRMED )) {
631
+ $ status = self ::STATUS_UNSUBSCRIBED ;
632
+ }
633
+
634
+ return $ status ;
635
+ }
636
+
600
637
/**
601
638
* Confirms subscriber newsletter
602
639
*
@@ -798,4 +835,18 @@ public function getSubscriberFullName()
798
835
}
799
836
return $ name ;
800
837
}
838
+
839
+ /**
840
+ * Set date of last changed status
841
+ *
842
+ * @return $this
843
+ */
844
+ public function beforeSave ()
845
+ {
846
+ parent ::beforeSave ();
847
+ if ($ this ->dataHasChangedFor ('subscriber_status ' )) {
848
+ $ this ->setChangeStatusAt ($ this ->dateTime ->gmtDate ());
849
+ }
850
+ return $ this ;
851
+ }
801
852
}
0 commit comments