Skip to content
Snippets Groups Projects
Commit 1bd05971 authored by Valeriy Nayda's avatar Valeriy Nayda
Browse files

Merge remote-tracking branch 'origin/MAGETWO-40063' into MAGETWO-35251

parents efc1a09d 3dfbc591
No related merge requests found
...@@ -6,29 +6,69 @@ ...@@ -6,29 +6,69 @@
*/ */
namespace Magento\Catalog\Controller\Adminhtml\Product; 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 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 * @return \Magento\Backend\Model\View\Result\Redirect
*/ */
public function execute() public function execute()
{ {
$productIds = $this->getRequest()->getParam('selected'); $selected = $this->getRequest()->getParam('selected');
if (!is_array($productIds) || empty($productIds)) { $excluded = $this->getRequest()->getParam('excluded');
$this->messageManager->addError(__('Please select product(s).'));
} else { $collection = $this->_objectManager->create($this->collection);
try { try {
foreach ($productIds as $productId) { if (!empty($excluded)) {
$product = $this->_objectManager->get('Magento\Catalog\Model\Product')->load($productId); $collection->addFieldToFilter(static::ID_FIELD, ['nin' => $excluded]);
$product->delete(); $this->massAction($collection);
} } elseif (!empty($selected)) {
$this->messageManager->addSuccess( $collection->addFieldToFilter(static::ID_FIELD, ['in' => $selected]);
__('A total of %1 record(s) have been deleted.', count($productIds)) $this->massAction($collection);
); } else {
} catch (\Exception $e) { $this->messageManager->addError(__('Please select product(s).'));
$this->messageManager->addError($e->getMessage());
} }
} 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