Skip to content

Commit b6c6a79

Browse files
authored
Merge pull request #2735 from magento-performance/MAGETWO-92693
[MAGETWO-92693] Some improvements on product create|edit page in admin area
2 parents 78ca274 + 41b2b1e commit b6c6a79

File tree

9 files changed

+392
-187
lines changed

9 files changed

+392
-187
lines changed

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

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Magento\Store\Model\StoreFactory;
1212
use Psr\Log\LoggerInterface as Logger;
1313
use Magento\Framework\Registry;
14+
use Magento\Catalog\Api\ProductRepositoryInterface;
15+
use Magento\Catalog\Model\Product;
16+
use Magento\Catalog\Model\Product\Type as ProductTypes;
1417

1518
class Builder
1619
{
@@ -39,6 +42,11 @@ class Builder
3942
*/
4043
protected $storeFactory;
4144

45+
/**
46+
* @var ProductRepositoryInterface
47+
*/
48+
private $productRepository;
49+
4250
/**
4351
* Constructor
4452
*
@@ -47,61 +55,87 @@ class Builder
4755
* @param Registry $registry
4856
* @param WysiwygModel\Config $wysiwygConfig
4957
* @param StoreFactory|null $storeFactory
58+
* @param ProductRepositoryInterface|null $productRepository
5059
*/
5160
public function __construct(
5261
ProductFactory $productFactory,
5362
Logger $logger,
5463
Registry $registry,
5564
WysiwygModel\Config $wysiwygConfig,
56-
StoreFactory $storeFactory = null
65+
StoreFactory $storeFactory = null,
66+
ProductRepositoryInterface $productRepository = null
5767
) {
5868
$this->productFactory = $productFactory;
5969
$this->logger = $logger;
6070
$this->registry = $registry;
6171
$this->wysiwygConfig = $wysiwygConfig;
6272
$this->storeFactory = $storeFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
6373
->get(\Magento\Store\Model\StoreFactory::class);
74+
$this->productRepository = $productRepository ?: \Magento\Framework\App\ObjectManager::getInstance()
75+
->get(ProductRepositoryInterface::class);
6476
}
6577

6678
/**
6779
* Build product based on user request
6880
*
6981
* @param RequestInterface $request
7082
* @return \Magento\Catalog\Model\Product
83+
* @throws \RuntimeException
7184
*/
7285
public function build(RequestInterface $request)
7386
{
74-
$productId = (int)$request->getParam('id');
75-
/** @var $product \Magento\Catalog\Model\Product */
76-
$product = $this->productFactory->create();
77-
$product->setStoreId($request->getParam('store', 0));
78-
$store = $this->storeFactory->create();
79-
$store->load($request->getParam('store', 0));
80-
87+
$productId = (int) $request->getParam('id');
88+
$storeId = $request->getParam('store', 0);
89+
$attributeSetId = (int) $request->getParam('set');
8190
$typeId = $request->getParam('type');
82-
if (!$productId && $typeId) {
83-
$product->setTypeId($typeId);
84-
}
8591

86-
$product->setData('_edit_mode', true);
8792
if ($productId) {
8893
try {
89-
$product->load($productId);
94+
$product = $this->productRepository->getById($productId, true, $storeId);
9095
} catch (\Exception $e) {
91-
$product->setTypeId(\Magento\Catalog\Model\Product\Type::DEFAULT_TYPE);
96+
$product = $this->createEmptyProduct(ProductTypes::DEFAULT_TYPE, $attributeSetId, $storeId);
9297
$this->logger->critical($e);
9398
}
99+
} else {
100+
$product = $this->createEmptyProduct($typeId, $attributeSetId, $storeId);
94101
}
95102

96-
$setId = (int)$request->getParam('set');
97-
if ($setId) {
98-
$product->setAttributeSetId($setId);
99-
}
103+
$store = $this->storeFactory->create();
104+
$store->load($storeId);
100105

101106
$this->registry->register('product', $product);
102107
$this->registry->register('current_product', $product);
103108
$this->registry->register('current_store', $store);
104-
$this->wysiwygConfig->setStoreId($request->getParam('store'));
109+
110+
$this->wysiwygConfig->setStoreId($storeId);
111+
112+
return $product;
113+
}
114+
115+
/**
116+
* @param int $typeId
117+
* @param int $attributeSetId
118+
* @param int $storeId
119+
* @return \Magento\Catalog\Model\Product
120+
*/
121+
private function createEmptyProduct($typeId, $attributeSetId, $storeId): Product
122+
{
123+
/** @var $product \Magento\Catalog\Model\Product */
124+
$product = $this->productFactory->create();
125+
$product->setData('_edit_mode', true);
126+
127+
if ($typeId !== null) {
128+
$product->setTypeId($typeId);
129+
}
130+
131+
if ($storeId !== null) {
132+
$product->setStoreId($storeId);
133+
}
134+
135+
if ($attributeSetId) {
136+
$product->setAttributeSetId($attributeSetId);
137+
}
138+
105139
return $product;
106140
}
107141
}

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,13 @@ public function get($sku, $editMode = false, $storeId = null, $forceReload = fal
234234
$cacheKey = $this->getCacheKey([$editMode, $storeId]);
235235
$cachedProduct = $this->getProductFromLocalCache($sku, $cacheKey);
236236
if ($cachedProduct === null || $forceReload) {
237-
$product = $this->productFactory->create();
238-
239237
$productId = $this->resourceModel->getIdBySku($sku);
240238
if (!$productId) {
241239
throw new NoSuchEntityException(__('Requested product doesn\'t exist'));
242240
}
243-
if ($editMode) {
244-
$product->setData('_edit_mode', true);
245-
}
246-
if ($storeId !== null) {
247-
$product->setData('store_id', $storeId);
248-
}
249-
$product->load($productId);
241+
242+
$product = $this->getById($productId, $editMode, $storeId, $forceReload);
243+
250244
$this->cacheProduct($cacheKey, $product);
251245
$cachedProduct = $product;
252246
}

0 commit comments

Comments
 (0)