|
6 | 6 | */
|
7 | 7 | namespace Magento\Catalog\Controller\Adminhtml\Product;
|
8 | 8 |
|
| 9 | +use Magento\Catalog\Model\Resource\Product\Collection; |
| 10 | +use Magento\Framework\Controller\ResultFactory; |
| 11 | + |
9 | 12 | class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product
|
10 | 13 | {
|
| 14 | + /** |
| 15 | + * Field id |
| 16 | + */ |
| 17 | + const ID_FIELD = 'entity_id'; |
| 18 | + |
| 19 | + /** |
| 20 | + * Redirect url |
| 21 | + */ |
| 22 | + const REDIRECT_URL = 'catalog/*/index'; |
| 23 | + |
| 24 | + /** |
| 25 | + * Resource collection |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $collection = 'Magento\Catalog\Model\Resource\Product\Collection'; |
| 30 | + |
11 | 31 | /**
|
12 | 32 | * @return \Magento\Backend\Model\View\Result\Redirect
|
13 | 33 | */
|
14 | 34 | public function execute()
|
15 | 35 | {
|
16 |
| - $productIds = $this->getRequest()->getParam('selected'); |
17 |
| - if (!is_array($productIds) || empty($productIds)) { |
18 |
| - $this->messageManager->addError(__('Please select product(s).')); |
19 |
| - } else { |
20 |
| - try { |
21 |
| - foreach ($productIds as $productId) { |
22 |
| - $product = $this->_objectManager->get('Magento\Catalog\Model\Product')->load($productId); |
23 |
| - $product->delete(); |
24 |
| - } |
25 |
| - $this->messageManager->addSuccess( |
26 |
| - __('A total of %1 record(s) have been deleted.', count($productIds)) |
27 |
| - ); |
28 |
| - } catch (\Exception $e) { |
29 |
| - $this->messageManager->addError($e->getMessage()); |
| 36 | + $selected = $this->getRequest()->getParam('selected'); |
| 37 | + $excluded = $this->getRequest()->getParam('excluded'); |
| 38 | + |
| 39 | + $collection = $this->_objectManager->create($this->collection); |
| 40 | + try { |
| 41 | + if (!empty($excluded)) { |
| 42 | + $collection->addFieldToFilter(static::ID_FIELD, ['nin' => $excluded]); |
| 43 | + $this->massAction($collection); |
| 44 | + } elseif (!empty($selected)) { |
| 45 | + $collection->addFieldToFilter(static::ID_FIELD, ['in' => $selected]); |
| 46 | + $this->massAction($collection); |
| 47 | + } else { |
| 48 | + $this->messageManager->addError(__('Please select product(s).')); |
30 | 49 | }
|
| 50 | + } catch (\Exception $e) { |
| 51 | + $this->messageManager->addError($e->getMessage()); |
| 52 | + } |
| 53 | + |
| 54 | + /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ |
| 55 | + $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); |
| 56 | + return $resultRedirect->setPath(static::REDIRECT_URL); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Cancel selected orders |
| 61 | + * |
| 62 | + * @param Collection $collection |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + protected function massAction($collection) |
| 66 | + { |
| 67 | + $count = 0; |
| 68 | + foreach ($collection->getItems() as $product) { |
| 69 | + $product->delete(); |
| 70 | + ++$count; |
31 | 71 | }
|
32 |
| - return $this->resultRedirectFactory->create()->setPath('catalog/*/index'); |
| 72 | + $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $count)); |
33 | 73 | }
|
34 | 74 | }
|
0 commit comments