Skip to content

Commit 5354836

Browse files
committed
MC-4242: Newsletter subscriptions per website
1 parent 107d68a commit 5354836

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Index/MassUnsubscribe.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
namespace Magento\Customer\Controller\Adminhtml\Index;
77

88
use Magento\Customer\Api\CustomerRepositoryInterface;
9+
use Magento\Customer\Api\Data\CustomerInterface;
10+
use Magento\Customer\Model\Config\Share;
911
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1012
use Magento\Framework\Controller\ResultFactory;
1113
use Magento\Backend\App\Action\Context;
1214
use Magento\Newsletter\Model\SubscriptionManagerInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
1316
use Magento\Ui\Component\MassAction\Filter;
1417
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
1518
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
@@ -29,23 +32,38 @@ class MassUnsubscribe extends AbstractMassAction implements HttpPostActionInterf
2932
*/
3033
private $subscriptionManager;
3134

35+
/**
36+
* @var StoreManagerInterface
37+
*/
38+
private $storeManager;
39+
40+
/**
41+
* @var Share
42+
*/
43+
private $shareConfig;
44+
3245
/**
3346
* @param Context $context
3447
* @param Filter $filter
3548
* @param CollectionFactory $collectionFactory
3649
* @param CustomerRepositoryInterface $customerRepository
3750
* @param SubscriptionManagerInterface $subscriptionManager
51+
* @param Share $shareConfig
3852
*/
3953
public function __construct(
4054
Context $context,
4155
Filter $filter,
4256
CollectionFactory $collectionFactory,
4357
CustomerRepositoryInterface $customerRepository,
44-
SubscriptionManagerInterface $subscriptionManager
58+
SubscriptionManagerInterface $subscriptionManager,
59+
StoreManagerInterface $storeManager,
60+
Share $shareConfig
4561
) {
4662
parent::__construct($context, $filter, $collectionFactory);
4763
$this->customerRepository = $customerRepository;
4864
$this->subscriptionManager = $subscriptionManager;
65+
$this->storeManager = $storeManager;
66+
$this->shareConfig = $shareConfig;
4967
}
5068

5169
/**
@@ -60,8 +78,9 @@ protected function massAction(AbstractCollection $collection)
6078
foreach ($collection->getAllIds() as $customerId) {
6179
// Verify customer exists
6280
$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+
}
6584
$customersUpdated++;
6685
}
6786

@@ -74,4 +93,24 @@ protected function massAction(AbstractCollection $collection)
7493

7594
return $resultRedirect;
7695
}
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+
}
77116
}

0 commit comments

Comments
 (0)