3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Newsletter \Model \Plugin ;
7
8
8
9
use Magento \Customer \Api \CustomerRepositoryInterface ;
10
+ use Magento \Customer \Api \Data \CustomerExtensionInterface ;
9
11
use Magento \Customer \Api \Data \CustomerInterface ;
10
12
use Magento \Customer \Model \Config \Share ;
11
13
use Magento \Framework \Api \ExtensionAttributesFactory ;
14
+ use Magento \Framework \Api \SearchResults ;
15
+ use Magento \Framework \App \ObjectManager ;
12
16
use Magento \Framework \Exception \NoSuchEntityException ;
13
- use Magento \Newsletter \Model \Subscriber ;
17
+ use Magento \Newsletter \Model \CustomerSubscriberCache ;
14
18
use Magento \Newsletter \Model \ResourceModel \Subscriber \CollectionFactory ;
15
- use Magento \Customer \ Api \ Data \ CustomerExtensionInterface ;
19
+ use Magento \Newsletter \ Model \ Subscriber ;
16
20
use Magento \Newsletter \Model \SubscriberFactory ;
17
21
use Magento \Newsletter \Model \SubscriptionManagerInterface ;
18
22
use Magento \Store \Model \Store ;
19
23
use Magento \Store \Model \StoreManagerInterface ;
20
- use Magento \Framework \Api \SearchResults ;
21
24
use Psr \Log \LoggerInterface ;
22
25
23
26
/**
@@ -62,6 +65,11 @@ class CustomerPlugin
62
65
*/
63
66
private $ logger ;
64
67
68
+ /**
69
+ * @var CustomerSubscriberCache
70
+ */
71
+ private $ customerSubscriberCache ;
72
+
65
73
/**
66
74
* @param SubscriberFactory $subscriberFactory
67
75
* @param ExtensionAttributesFactory $extensionFactory
@@ -70,6 +78,7 @@ class CustomerPlugin
70
78
* @param Share $shareConfig
71
79
* @param StoreManagerInterface $storeManager
72
80
* @param LoggerInterface $logger
81
+ * @param CustomerSubscriberCache|null $customerSubscriberCache
73
82
*/
74
83
public function __construct (
75
84
SubscriberFactory $ subscriberFactory ,
@@ -78,7 +87,8 @@ public function __construct(
78
87
SubscriptionManagerInterface $ subscriptionManager ,
79
88
Share $ shareConfig ,
80
89
StoreManagerInterface $ storeManager ,
81
- LoggerInterface $ logger
90
+ LoggerInterface $ logger ,
91
+ CustomerSubscriberCache $ customerSubscriberCache = null
82
92
) {
83
93
$ this ->subscriberFactory = $ subscriberFactory ;
84
94
$ this ->extensionFactory = $ extensionFactory ;
@@ -87,6 +97,8 @@ public function __construct(
87
97
$ this ->shareConfig = $ shareConfig ;
88
98
$ this ->storeManager = $ storeManager ;
89
99
$ this ->logger = $ logger ;
100
+ $ this ->customerSubscriberCache = $ customerSubscriberCache
101
+ ?? ObjectManager::getInstance ()->get (CustomerSubscriberCache::class);
90
102
}
91
103
92
104
/**
@@ -124,9 +136,11 @@ public function afterSave(
124
136
}
125
137
if ($ needToUpdate ) {
126
138
$ storeId = $ this ->getCurrentStoreId ($ result );
139
+ $ customerId = (int )$ result ->getId ();
127
140
$ subscriber = $ subscribeStatus
128
- ? $ this ->subscriptionManager ->subscribeCustomer ((int )$ result ->getId (), $ storeId )
129
- : $ this ->subscriptionManager ->unsubscribeCustomer ((int )$ result ->getId (), $ storeId );
141
+ ? $ this ->subscriptionManager ->subscribeCustomer ($ customerId , $ storeId )
142
+ : $ this ->subscriptionManager ->unsubscribeCustomer ($ customerId , $ storeId );
143
+ $ this ->customerSubscriberCache ->setCustomerSubscriber ($ customerId , $ subscriber );
130
144
}
131
145
$ this ->addIsSubscribedExtensionAttribute ($ result , $ subscriber ->isSubscribed ());
132
146
@@ -252,7 +266,7 @@ public function afterGetList(CustomerRepositoryInterface $subject, SearchResults
252
266
$ extensionAttributes = $ customer ->getExtensionAttributes ();
253
267
/** @var Subscriber $subscribe */
254
268
$ subscribe = $ collection ->getItemByColumnValue ('subscriber_email ' , $ customer ->getEmail ());
255
- $ isSubscribed = $ subscribe && (int ) $ subscribe ->getStatus () === Subscriber::STATUS_SUBSCRIBED ;
269
+ $ isSubscribed = $ subscribe && (int )$ subscribe ->getStatus () === Subscriber::STATUS_SUBSCRIBED ;
256
270
$ extensionAttributes ->setIsSubscribed ($ isSubscribed );
257
271
}
258
272
@@ -308,16 +322,20 @@ private function deleteSubscriptionsAfterCustomerDelete(CustomerInterface $custo
308
322
*/
309
323
private function getSubscriber (CustomerInterface $ customer ): Subscriber
310
324
{
311
- /** @var Subscriber $subscriber */
312
- $ subscriber = $ this ->subscriberFactory ->create ();
313
- $ websiteId = $ this ->getCurrentWebsiteId ($ customer );
314
- $ subscriber ->loadByCustomer ((int )$ customer ->getId (), $ websiteId );
315
- /**
316
- * If subscriber was't found by customer id then try to find subscriber by customer email.
317
- * It need when the customer is creating and he has already subscribed as guest by same email.
318
- */
319
- if (!$ subscriber ->getId ()) {
320
- $ subscriber ->loadBySubscriberEmail ((string )$ customer ->getEmail (), $ websiteId );
325
+ $ customerId = (int )$ customer ->getId ();
326
+ $ subscriber = $ this ->customerSubscriberCache ->getCustomerSubscriber ($ customerId );
327
+ if ($ subscriber === null ) {
328
+ $ subscriber = $ this ->subscriberFactory ->create ();
329
+ $ websiteId = $ this ->getCurrentWebsiteId ($ customer );
330
+ $ subscriber ->loadByCustomer ((int )$ customer ->getId (), $ websiteId );
331
+ /**
332
+ * If subscriber wasn't found by customer id then try to find subscriber by customer email.
333
+ * It need when the customer is creating and he has already subscribed as guest by same email.
334
+ */
335
+ if (!$ subscriber ->getId ()) {
336
+ $ subscriber ->loadBySubscriberEmail ((string )$ customer ->getEmail (), $ websiteId );
337
+ }
338
+ $ this ->customerSubscriberCache ->setCustomerSubscriber ($ customerId , $ subscriber );
321
339
}
322
340
323
341
return $ subscriber ;
0 commit comments