Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassDelete.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 
21 class MassDelete extends Action implements HttpPostActionInterface
22 {
26  const ADMIN_RESOURCE = 'Magento_InventoryApi::stock';
27 
31  private $stockRepository;
32 
36  private $massActionFilter;
37 
43  public function __construct(
44  Context $context,
45  StockRepositoryInterface $stockRepository,
46  Filter $massActionFilter
47  ) {
48  parent::__construct($context);
49  $this->stockRepository = $stockRepository;
50  $this->massActionFilter = $massActionFilter;
51  }
52 
56  public function execute(): ResultInterface
57  {
58  if ($this->getRequest()->isPost() !== true) {
59  $this->messageManager->addErrorMessage(__('Wrong request.'));
60 
61  return $this->resultRedirectFactory->create()->setPath('*/*');
62  }
63 
64  $deletedItemsCount = 0;
65  foreach ($this->massActionFilter->getIds() as $id) {
66  try {
67  $id = (int)$id;
68  $this->stockRepository->deleteById($id);
69  $deletedItemsCount++;
70  } catch (CouldNotDeleteException $e) {
71  $errorMessage = __('[ID: %1] ', $id) . $e->getMessage();
72  $this->messageManager->addErrorMessage($errorMessage);
73  }
74  }
75  $this->messageManager->addSuccessMessage(__('You deleted %1 Stock(s).', $deletedItemsCount));
76 
77  return $this->resultRedirectFactory->create()->setPath('*/*');
78  }
79 }
__construct(Context $context, StockRepositoryInterface $stockRepository, Filter $massActionFilter)
Definition: MassDelete.php:43
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$stockRepository
Definition: stock.php:19