6
6
namespace Magento \Customer \Controller \Adminhtml \Index ;
7
7
8
8
use Magento \Customer \Api \CustomerRepositoryInterface ;
9
+ use Magento \Customer \Api \Data \CustomerInterface ;
10
+ use Magento \Customer \Model \Config \Share ;
9
11
use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
10
12
use Magento \Framework \Controller \ResultFactory ;
11
13
use Magento \Backend \App \Action \Context ;
12
14
use Magento \Newsletter \Model \SubscriptionManagerInterface ;
15
+ use Magento \Store \Model \StoreManagerInterface ;
13
16
use Magento \Ui \Component \MassAction \Filter ;
14
17
use Magento \Customer \Model \ResourceModel \Customer \CollectionFactory ;
15
18
use Magento \Eav \Model \Entity \Collection \AbstractCollection ;
@@ -29,23 +32,38 @@ class MassUnsubscribe extends AbstractMassAction implements HttpPostActionInterf
29
32
*/
30
33
private $ subscriptionManager ;
31
34
35
+ /**
36
+ * @var StoreManagerInterface
37
+ */
38
+ private $ storeManager ;
39
+
40
+ /**
41
+ * @var Share
42
+ */
43
+ private $ shareConfig ;
44
+
32
45
/**
33
46
* @param Context $context
34
47
* @param Filter $filter
35
48
* @param CollectionFactory $collectionFactory
36
49
* @param CustomerRepositoryInterface $customerRepository
37
50
* @param SubscriptionManagerInterface $subscriptionManager
51
+ * @param Share $shareConfig
38
52
*/
39
53
public function __construct (
40
54
Context $ context ,
41
55
Filter $ filter ,
42
56
CollectionFactory $ collectionFactory ,
43
57
CustomerRepositoryInterface $ customerRepository ,
44
- SubscriptionManagerInterface $ subscriptionManager
58
+ SubscriptionManagerInterface $ subscriptionManager ,
59
+ StoreManagerInterface $ storeManager ,
60
+ Share $ shareConfig
45
61
) {
46
62
parent ::__construct ($ context , $ filter , $ collectionFactory );
47
63
$ this ->customerRepository = $ customerRepository ;
48
64
$ this ->subscriptionManager = $ subscriptionManager ;
65
+ $ this ->storeManager = $ storeManager ;
66
+ $ this ->shareConfig = $ shareConfig ;
49
67
}
50
68
51
69
/**
@@ -60,8 +78,9 @@ protected function massAction(AbstractCollection $collection)
60
78
foreach ($ collection ->getAllIds () as $ customerId ) {
61
79
// Verify customer exists
62
80
$ customer = $ this ->customerRepository ->getById ($ customerId );
63
- $ storeId = (int )$ customer ->getStoreId ();
64
- $ this ->subscriptionManager ->unsubscribeCustomer ($ customerId , $ storeId );
81
+ foreach ($ this ->getUnsubscribeStoreIds ($ customer ) as $ storeId ) {
82
+ $ this ->subscriptionManager ->unsubscribeCustomer ((int )$ customerId , $ storeId );
83
+ }
65
84
$ customersUpdated ++;
66
85
}
67
86
@@ -74,4 +93,24 @@ protected function massAction(AbstractCollection $collection)
74
93
75
94
return $ resultRedirect ;
76
95
}
96
+
97
+ /**
98
+ * Get store ids to unsubscribe customer
99
+ *
100
+ * @param CustomerInterface $customer
101
+ * @return array
102
+ */
103
+ private function getUnsubscribeStoreIds (CustomerInterface $ customer ): array
104
+ {
105
+ $ storeIds = [];
106
+ if ($ this ->shareConfig ->isGlobalScope ()) {
107
+ foreach ($ this ->storeManager ->getStores () as $ store ) {
108
+ $ storeIds [(int )$ store ->getWebsiteId ()] = (int )$ store ->getId ();
109
+ }
110
+ } else {
111
+ $ storeIds = [(int )$ customer ->getStoreId ()];
112
+ }
113
+
114
+ return $ storeIds ;
115
+ }
77
116
}
0 commit comments