Skip to content
Snippets Groups Projects
Commit 3dfbc591 authored by Vlad Veselov's avatar Vlad Veselov
Browse files

MAGETWO-40063: Impossible to perform mass delete using "Select All" functionality

parent c36b15cf
No related merge requests found
......@@ -6,29 +6,69 @@
*/
namespace Magento\Catalog\Controller\Adminhtml\Product;
use Magento\Catalog\Model\Resource\Product\Collection;
use Magento\Framework\Controller\ResultFactory;
class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product
{
/**
* Field id
*/
const ID_FIELD = 'entity_id';
/**
* Redirect url
*/
const REDIRECT_URL = 'catalog/*/index';
/**
* Resource collection
*
* @var string
*/
protected $collection = 'Magento\Catalog\Model\Resource\Product\Collection';
/**
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
$productIds = $this->getRequest()->getParam('selected');
if (!is_array($productIds) || empty($productIds)) {
$this->messageManager->addError(__('Please select product(s).'));
} else {
try {
foreach ($productIds as $productId) {
$product = $this->_objectManager->get('Magento\Catalog\Model\Product')->load($productId);
$product->delete();
}
$this->messageManager->addSuccess(
__('A total of %1 record(s) have been deleted.', count($productIds))
);
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
$selected = $this->getRequest()->getParam('selected');
$excluded = $this->getRequest()->getParam('excluded');
$collection = $this->_objectManager->create($this->collection);
try {
if (!empty($excluded)) {
$collection->addFieldToFilter(static::ID_FIELD, ['nin' => $excluded]);
$this->massAction($collection);
} elseif (!empty($selected)) {
$collection->addFieldToFilter(static::ID_FIELD, ['in' => $selected]);
$this->massAction($collection);
} else {
$this->messageManager->addError(__('Please select product(s).'));
}
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
}
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath(static::REDIRECT_URL);
}
/**
* Cancel selected orders
*
* @param Collection $collection
* @return void
*/
protected function massAction($collection)
{
$count = 0;
foreach ($collection->getItems() as $product) {
$product->delete();
++$count;
}
return $this->resultRedirectFactory->create()->setPath('catalog/*/index');
$this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $count));
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment