Skip to content

Commit 8b1ad1c

Browse files
committed
MC-41093: [CE/EE] Fatal error during saving Simple Product by admin
1 parent 45f27d6 commit 8b1ad1c

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

app/code/Magento/CatalogInventory/Model/Stock/StockItemRepository.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Magento\Framework\Stdlib\DateTime\DateTime;
2626
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
2727
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
28+
use Psr\Log\LoggerInterface as PsrLogger;
2829

2930
/**
3031
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -98,8 +99,11 @@ class StockItemRepository implements StockItemRepositoryInterface
9899
protected $productCollectionFactory;
99100

100101
/**
101-
* Constructor
102-
*
102+
* @var PsrLogger
103+
*/
104+
private $psrLogger;
105+
106+
/**
103107
* @param StockConfigurationInterface $stockConfiguration
104108
* @param StockStateProviderInterface $stockStateProvider
105109
* @param StockItemResource $resource
@@ -111,7 +115,8 @@ class StockItemRepository implements StockItemRepositoryInterface
111115
* @param TimezoneInterface $localeDate
112116
* @param Processor $indexProcessor
113117
* @param DateTime $dateTime
114-
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory|null $collectionFactory
118+
* @param CollectionFactory|null $productCollectionFactory
119+
* @param PsrLogger|null $psrLogger
115120
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
116121
*/
117122
public function __construct(
@@ -126,7 +131,8 @@ public function __construct(
126131
TimezoneInterface $localeDate,
127132
Processor $indexProcessor,
128133
DateTime $dateTime,
129-
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory = null
134+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory = null,
135+
PsrLogger $psrLogger = null
130136
) {
131137
$this->stockConfiguration = $stockConfiguration;
132138
$this->stockStateProvider = $stockStateProvider;
@@ -141,6 +147,8 @@ public function __construct(
141147
$this->dateTime = $dateTime;
142148
$this->productCollectionFactory = $productCollectionFactory ?: ObjectManager::getInstance()
143149
->get(CollectionFactory::class);
150+
$this->psrLogger = $psrLogger ?: ObjectManager::getInstance()
151+
->get(PsrLogger::class);
144152
}
145153

146154
/**
@@ -184,6 +192,7 @@ public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stoc
184192

185193
$this->resource->save($stockItem);
186194
} catch (\Exception $exception) {
195+
$this->psrLogger->error($exception->getMessage());
187196
throw new CouldNotSaveException(__('The stock item was unable to be saved. Please try again.'), $exception);
188197
}
189198
return $stockItem;

app/code/Magento/Elasticsearch/Model/ResourceModel/Index.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Elasticsearch\Model\ResourceModel;
77

88
use Magento\Catalog\Model\Indexer\Product\Price\DimensionCollectionFactory;
9+
use Magento\Framework\Exception\NoSuchEntityException;
910
use Magento\Framework\Model\ResourceModel\Db\Context;
1011
use Magento\Store\Model\StoreManagerInterface;
1112
use Magento\Catalog\Api\ProductRepositoryInterface;
@@ -137,7 +138,11 @@ public function getFullCategoryProductIndexData($storeId = null, $productIds = n
137138

138139
foreach ($categoryPositions as $productId => $positions) {
139140
foreach ($positions as $categoryId => $position) {
140-
$category = $this->categoryRepository->get($categoryId, $storeId);
141+
try {
142+
$category = $this->categoryRepository->get($categoryId, $storeId);
143+
} catch (NoSuchEntityException $e) {
144+
continue;
145+
}
141146
$categoryName = $category->getName();
142147
$categoryData[$productId][] = [
143148
'id' => $categoryId,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Controller\Adminhtml\Product\Save;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\App\Request\Http as HttpRequest;
12+
use Magento\Framework\Message\MessageInterface;
13+
use Magento\TestFramework\TestCase\AbstractBackendController;
14+
15+
/**
16+
* @magentoDbIsolation disabled
17+
* @magentoDataFixture Magento/Catalog/_files/category_product.php
18+
* @magentoAppArea adminhtml
19+
*/
20+
class DeleteCategoryTest extends AbstractBackendController
21+
{
22+
/**
23+
* @var ProductRepositoryInterface
24+
*/
25+
private $productRepository;
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
34+
35+
}
36+
37+
/**
38+
* Checks product saving with deleted category before reindex is done
39+
*/
40+
public function testDeleteCustomOptionWithTypeField(): void
41+
{
42+
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class);
43+
$category->load(333);
44+
$category->delete();
45+
$product = $this->productRepository->get('simple333');
46+
$this->productRepository->save($product);
47+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
48+
$this->dispatch('backend/catalog/product/save/id/' . $product->getEntityId());
49+
$this->assertSessionMessages(
50+
$this->equalTo([(string)__('You saved the product.')]),
51+
MessageInterface::TYPE_SUCCESS
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)