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,29 @@ 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
+ if ($ newExtensionAttributes ->getIsSubscribed () === true ) {
86
+ $ subscriber ->subscribeCustomerById ($ resultId );
87
+ } elseif ($ newExtensionAttributes ->getIsSubscribed () === false ) {
88
+ $ subscriber ->unsubscribeCustomerById ($ resultId );
50
89
}
51
90
}
91
+ $ isSubscribed = $ subscriber ->isSubscribed ();
92
+ $ this ->customerSubscriptionStatus [$ resultId ] = $ isSubscribed ;
93
+ $ initialExtensionAttributes ->setIsSubscribed ($ isSubscribed );
52
94
return $ result ;
53
95
}
54
96
@@ -94,4 +136,49 @@ public function afterDelete(CustomerRepository $subject, $result, CustomerInterf
94
136
}
95
137
return $ result ;
96
138
}
139
+
140
+ /**
141
+ * Plugin after getById customer that obtains newsletter subscription status for given customer.
142
+ *
143
+ * @param CustomerRepository $subject
144
+ * @param CustomerInterface $customer
145
+ * @return CustomerInterface
146
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
147
+ */
148
+ public function afterGetById (CustomerRepository $ subject , CustomerInterface $ customer )
149
+ {
150
+ $ extensionAttributes = $ customer ->getExtensionAttributes ();
151
+ if ($ extensionAttributes === null ) {
152
+ /** @var CustomerExtensionInterface $customerExtension */
153
+ $ customerExtension = $ this ->extensionFactory ->create (CustomerInterface::class);
154
+ $ isSubscribed = $ this ->isSubscribed ($ customer );
155
+ $ customerExtension ->setIsSubscribed ($ isSubscribed );
156
+ $ customer ->setExtensionAttributes ($ customerExtension );
157
+ return $ customer ;
158
+ }
159
+ if ($ extensionAttributes ->getIsSubscribed () === null ) {
160
+ $ isSubscribed = $ this ->isSubscribed ($ customer );
161
+ $ extensionAttributes ->setIsSubscribed ($ isSubscribed );
162
+ $ customer ->setExtensionAttributes ($ extensionAttributes );
163
+ return $ customer ;
164
+ }
165
+ return $ customer ;
166
+ }
167
+
168
+ /**
169
+ * This method returns newsletters subscription status for given customer.
170
+ *
171
+ * @param CustomerInterface $customer
172
+ * @return mixed
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
+ return $ this ->customerSubscriptionStatus [$ customerId ];
183
+ }
97
184
}
0 commit comments