Skip to content

Commit 1bd0597

Browse files
author
Valeriy Nayda
committed
Merge remote-tracking branch 'origin/MAGETWO-40063' into MAGETWO-35251
2 parents efc1a09 + 3dfbc59 commit 1bd0597

File tree

1 file changed

+55
-15
lines changed

1 file changed

+55
-15
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/MassDelete.php

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,69 @@
66
*/
77
namespace Magento\Catalog\Controller\Adminhtml\Product;
88

9+
use Magento\Catalog\Model\Resource\Product\Collection;
10+
use Magento\Framework\Controller\ResultFactory;
11+
912
class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product
1013
{
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+
1131
/**
1232
* @return \Magento\Backend\Model\View\Result\Redirect
1333
*/
1434
public function execute()
1535
{
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).'));
3049
}
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;
3171
}
32-
return $this->resultRedirectFactory->create()->setPath('catalog/*/index');
72+
$this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $count));
3373
}
3474
}

0 commit comments

Comments
 (0)