Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassUnsubscribe.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Newsletter\Model\SubscriberFactory;
13 use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
15 
20 {
25 
29  protected $subscriberFactory;
30 
38  public function __construct(
39  Context $context,
41  CollectionFactory $collectionFactory,
43  SubscriberFactory $subscriberFactory
44  ) {
45  parent::__construct($context, $filter, $collectionFactory);
46  $this->customerRepository = $customerRepository;
47  $this->subscriberFactory = $subscriberFactory;
48  }
49 
56  protected function massAction(AbstractCollection $collection)
57  {
58  $customersUpdated = 0;
59  foreach ($collection->getAllIds() as $customerId) {
60  // Verify customer exists
61  $this->customerRepository->getById($customerId);
62  $this->subscriberFactory->create()->unsubscribeCustomerById($customerId);
63  $customersUpdated++;
64  }
65 
66  if ($customersUpdated) {
67  $this->messageManager->addSuccess(__('A total of %1 record(s) were updated.', $customersUpdated));
68  }
70  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
71  $resultRedirect->setPath($this->getComponentRefererUrl());
72 
73  return $resultRedirect;
74  }
75 }
__()
Definition: __.php:13
__construct(Context $context, Filter $filter, CollectionFactory $collectionFactory, CustomerRepositoryInterface $customerRepository, SubscriberFactory $subscriberFactory)