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