Skip to content

Commit 1c6adbe

Browse files
ENGCOM-5519: Resolve MassDelete Product will have'the counter' wrong if an EXCEPTION happens #23939
- Merge Pull Request #23939 from edenduong/magento2:2.3-bugfix/process_throw_exception - Merged commits: 1. 30f5581 2. 7ca5932 3. 71b6a7e 4. 78a2c51 5. ea5d411 6. 7bcb47e 7. 8f9246f
2 parents d3c55bb + 8f9246f commit 1c6adbe

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
declare(strict_types=1);
8+
79
namespace Magento\Catalog\Controller\Adminhtml\Product;
810

911
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
@@ -12,7 +14,14 @@
1214
use Magento\Ui\Component\MassAction\Filter;
1315
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
1416
use Magento\Catalog\Api\ProductRepositoryInterface;
17+
use Magento\Framework\Exception\CouldNotSaveException;
18+
use Magento\Framework\Exception\StateException;
19+
use Magento\Framework\Exception\LocalizedException;
20+
use Psr\Log\LoggerInterface;
1521

22+
/**
23+
* Class \Magento\Catalog\Controller\Adminhtml\Product\MassDelete
24+
*/
1625
class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product implements HttpPostActionInterface
1726
{
1827
/**
@@ -32,38 +41,55 @@ class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product implement
3241
*/
3342
private $productRepository;
3443

44+
/**
45+
* @var LoggerInterface
46+
*/
47+
private $logger;
48+
3549
/**
3650
* @param Context $context
3751
* @param Builder $productBuilder
3852
* @param Filter $filter
3953
* @param CollectionFactory $collectionFactory
4054
* @param ProductRepositoryInterface $productRepository
55+
* @param LoggerInterface $logger
4156
*/
4257
public function __construct(
4358
Context $context,
4459
Builder $productBuilder,
4560
Filter $filter,
4661
CollectionFactory $collectionFactory,
47-
ProductRepositoryInterface $productRepository = null
62+
ProductRepositoryInterface $productRepository = null,
63+
LoggerInterface $logger = null
4864
) {
4965
$this->filter = $filter;
5066
$this->collectionFactory = $collectionFactory;
51-
$this->productRepository = $productRepository
52-
?: \Magento\Framework\App\ObjectManager::getInstance()->create(ProductRepositoryInterface::class);
67+
$this->productRepository = $productRepository ?:
68+
\Magento\Framework\App\ObjectManager::getInstance()->create(ProductRepositoryInterface::class);
69+
$this->logger = $logger ?:
70+
\Magento\Framework\App\ObjectManager::getInstance()->create(LoggerInterface::class);
5371
parent::__construct($context, $productBuilder);
5472
}
5573

5674
/**
75+
* Mass Delete Action
76+
*
5777
* @return \Magento\Backend\Model\View\Result\Redirect
5878
*/
5979
public function execute()
6080
{
6181
$collection = $this->filter->getCollection($this->collectionFactory->create());
6282
$productDeleted = 0;
83+
$productDeletedError = 0;
6384
/** @var \Magento\Catalog\Model\Product $product */
6485
foreach ($collection->getItems() as $product) {
65-
$this->productRepository->delete($product);
66-
$productDeleted++;
86+
try {
87+
$this->productRepository->delete($product);
88+
$productDeleted++;
89+
} catch (LocalizedException $exception) {
90+
$this->logger->error($exception->getLogMessage());
91+
$productDeletedError++;
92+
}
6793
}
6894

6995
if ($productDeleted) {
@@ -72,6 +98,15 @@ public function execute()
7298
);
7399
}
74100

101+
if ($productDeletedError) {
102+
$this->messageManager->addErrorMessage(
103+
__(
104+
'A total of %1 record(s) haven\'t been deleted. Please see server logs for more details.',
105+
$productDeletedError
106+
)
107+
);
108+
}
109+
75110
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('catalog/*/index');
76111
}
77112
}

app/code/Magento/Catalog/i18n/en_US.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,5 @@ Details,Details
810810
"Category with ID: (%1) doesn't exist", "Category with ID: (%1) doesn't exist"
811811
"You added product %1 to the <a href=""%2"">comparison list</a>.","You added product %1 to the <a href=""%2"">comparison list</a>."
812812
"Edit Product Design","Edit Product Design"
813-
"Edit Category Design","Edit Category Design"
813+
"Edit Category Design","Edit Category Design"
814+
"A total of %1 record(s) haven't been deleted. Please see server logs for more details.","A total of %1 record(s) haven't been deleted. Please see server logs for more details."

0 commit comments

Comments
 (0)