Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassAssignGroup.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
10 use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
15 
20 {
25 
32  public function __construct(
33  Context $context,
35  CollectionFactory $collectionFactory,
37  ) {
38  parent::__construct($context, $filter, $collectionFactory);
39  $this->customerRepository = $customerRepository;
40  }
41 
48  protected function massAction(AbstractCollection $collection)
49  {
50  $customersUpdated = 0;
51  foreach ($collection->getAllIds() as $customerId) {
52  // Verify customer exists
53  $customer = $this->customerRepository->getById($customerId);
54  $customer->setGroupId($this->getRequest()->getParam('group'));
55  $this->customerRepository->save($customer);
56  $customersUpdated++;
57  }
58 
59  if ($customersUpdated) {
60  $this->messageManager->addSuccess(__('A total of %1 record(s) were updated.', $customersUpdated));
61  }
63  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
64  $resultRedirect->setPath($this->getComponentRefererUrl());
65 
66  return $resultRedirect;
67  }
68 }
$customer
Definition: customers.php:11
__()
Definition: __.php:13
__construct(Context $context, Filter $filter, CollectionFactory $collectionFactory, CustomerRepositoryInterface $customerRepository)