8
8
use Magento \Customer \Api \CustomerRepositoryInterface as CustomerRepository ;
9
9
use Magento \Customer \Api \Data \CustomerInterface ;
10
10
use Magento \Newsletter \Model \SubscriberFactory ;
11
+ use Magento \Framework \Api \ExtensionAttributesFactory ;
12
+ use Magento \Newsletter \Model \ResourceModel \Subscriber ;
13
+ use Magento \Customer \Api \Data \CustomerExtensionInterface ;
14
+ use Magento \Framework \App \ObjectManager ;
11
15
12
16
class CustomerPlugin
13
17
{
@@ -18,14 +22,37 @@ class CustomerPlugin
18
22
*/
19
23
private $ subscriberFactory ;
20
24
25
+ /**
26
+ * @var ExtensionAttributesFactory
27
+ */
28
+ private $ extensionFactory ;
29
+
30
+ /**
31
+ * @var Subscriber
32
+ */
33
+ private $ subscriberResource ;
34
+
35
+ /**
36
+ * @var array
37
+ */
38
+ private $ customerSubscriptionStatus = [];
39
+
21
40
/**
22
41
* Initialize dependencies.
23
42
*
24
43
* @param SubscriberFactory $subscriberFactory
44
+ * @param ExtensionAttributesFactory|null $extensionFactory
45
+ * @param Subscriber|null $subscriberResource
25
46
*/
26
- public function __construct (SubscriberFactory $ subscriberFactory )
27
- {
47
+ public function __construct (
48
+ SubscriberFactory $ subscriberFactory ,
49
+ ExtensionAttributesFactory $ extensionFactory = null ,
50
+ Subscriber $ subscriberResource = null
51
+ ) {
28
52
$ this ->subscriberFactory = $ subscriberFactory ;
53
+ $ this ->extensionFactory = $ extensionFactory
54
+ ?: ObjectManager::getInstance ()->get (ExtensionAttributesFactory::class);
55
+ $ this ->subscriberResource = $ subscriberResource ?: ObjectManager::getInstance ()->get (Subscriber::class);
29
56
}
30
57
31
58
/**
@@ -41,14 +68,30 @@ public function __construct(SubscriberFactory $subscriberFactory)
41
68
*/
42
69
public function afterSave (CustomerRepository $ subject , CustomerInterface $ result , CustomerInterface $ customer )
43
70
{
44
- $ this ->subscriberFactory ->create ()->updateSubscription ($ result ->getId ());
45
- if ($ result ->getId () && $ customer ->getExtensionAttributes ()) {
46
- if ($ customer ->getExtensionAttributes ()->getIsSubscribed () === true ) {
47
- $ this ->subscriberFactory ->create ()->subscribeCustomerById ($ result ->getId ());
48
- } elseif ($ customer ->getExtensionAttributes ()->getIsSubscribed () === false ) {
49
- $ this ->subscriberFactory ->create ()->unsubscribeCustomerById ($ result ->getId ());
71
+ $ resultId = $ result ->getId ();
72
+ /** @var \Magento\Newsletter\Model\Subscriber $subscriber */
73
+ $ subscriber = $ this ->subscriberFactory ->create ();
74
+ $ subscriber ->updateSubscription ($ resultId );
75
+ // update the result only if the original customer instance had different value.
76
+ $ initialExtensionAttributes = $ result ->getExtensionAttributes ();
77
+ if ($ initialExtensionAttributes === null ) {
78
+ /** @var CustomerExtensionInterface $initialExtensionAttributes */
79
+ $ initialExtensionAttributes = $ this ->extensionFactory ->create (CustomerInterface::class);
80
+ $ result ->setExtensionAttributes ($ initialExtensionAttributes );
81
+ }
82
+ $ newExtensionAttributes = $ customer ->getExtensionAttributes ();
83
+ if ($ newExtensionAttributes
84
+ && $ initialExtensionAttributes ->getIsSubscribed () !== $ newExtensionAttributes ->getIsSubscribed ()
85
+ ) {
86
+ if ($ newExtensionAttributes ->getIsSubscribed () === true ) {
87
+ $ subscriber ->subscribeCustomerById ($ resultId );
88
+ } elseif ($ newExtensionAttributes ->getIsSubscribed () === false ) {
89
+ $ subscriber ->unsubscribeCustomerById ($ resultId );
50
90
}
51
91
}
92
+ $ isSubscribed = $ subscriber ->isSubscribed ();
93
+ $ this ->customerSubscriptionStatus [$ resultId ] = $ isSubscribed ;
94
+ $ initialExtensionAttributes ->setIsSubscribed ($ isSubscribed );
52
95
return $ result ;
53
96
}
54
97
@@ -94,4 +137,44 @@ public function afterDelete(CustomerRepository $subject, $result, CustomerInterf
94
137
}
95
138
return $ result ;
96
139
}
140
+
141
+ /**
142
+ * Plugin after getById customer that obtains newsletter subscription status for given customer.
143
+ *
144
+ * @param CustomerRepository $subject
145
+ * @param CustomerInterface $customer
146
+ * @return CustomerInterface
147
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
148
+ */
149
+ public function afterGetById (CustomerRepository $ subject , CustomerInterface $ customer )
150
+ {
151
+ $ extensionAttributes = $ customer ->getExtensionAttributes ();
152
+ if ($ extensionAttributes === null ) {
153
+ /** @var CustomerExtensionInterface $extensionAttributes */
154
+ $ extensionAttributes = $ this ->extensionFactory ->create (CustomerInterface::class);
155
+ $ customer ->setExtensionAttributes ($ extensionAttributes );
156
+ }
157
+ if ($ extensionAttributes ->getIsSubscribed () === null ) {
158
+ $ isSubscribed = $ this ->isSubscribed ($ customer );
159
+ $ extensionAttributes ->setIsSubscribed ($ isSubscribed );
160
+ }
161
+ return $ customer ;
162
+ }
163
+
164
+ /**
165
+ * This method returns newsletters subscription status for given customer.
166
+ *
167
+ * @param CustomerInterface $customer
168
+ * @return mixed
169
+ */
170
+ private function isSubscribed (CustomerInterface $ customer )
171
+ {
172
+ $ customerId = $ customer ->getId ();
173
+ if (!isset ($ this ->customerSubscriptionStatus [$ customerId ])) {
174
+ $ subscriber = $ this ->subscriberResource ->loadByCustomerData ($ customer );
175
+ $ this ->customerSubscriptionStatus [$ customerId ] = isset ($ subscriber ['subscriber_status ' ])
176
+ && $ subscriber ['subscriber_status ' ] == 1 ;
177
+ }
178
+ return $ this ->customerSubscriptionStatus [$ customerId ];
179
+ }
97
180
}
0 commit comments