Skip to content

Commit fa6782f

Browse files
committed
6486: #6486: Unable to save certain product properties via Rest API
1 parent 506d500 commit fa6782f

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ protected function initializeProductData(array $productData, $createNew)
329329
unset($productData['media_gallery']);
330330
if ($createNew) {
331331
$product = $this->productFactory->create();
332+
if (!isset($productData['product_type'])) {
333+
$product->setTypeId(Product\Type::TYPE_SIMPLE);
334+
}
332335
if ($this->storeManager->hasSingleStore()) {
333336
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]);
334337
}

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,32 @@ public function testCreateInvalidPriceFormat()
303303
}
304304
}
305305

306+
/**
307+
* Test that Product Repository can correctly create simple product, if product type not specified in request.
308+
*
309+
* @return void
310+
*/
311+
public function testCreateWithoutSpecifiedType()
312+
{
313+
$price = 3.62;
314+
$weight = 12.2;
315+
$sku = 'simple_product_without_specified_type';
316+
$product = [
317+
'sku' => '' . $sku . '',
318+
'name' => 'Simple Product Without Specified Type',
319+
'price' => $price,
320+
'weight' => $weight,
321+
'attribute_set_id' => 4,
322+
];
323+
$response = $this->saveProduct($product);
324+
$this->assertSame($sku, $response[ProductInterface::SKU]);
325+
$this->assertSame(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE, $response[ProductInterface::TYPE_ID]);
326+
$this->assertSame($price, $response[ProductInterface::PRICE]);
327+
$this->assertSame($weight, $response[ProductInterface::WEIGHT]);
328+
//Clean up.
329+
$this->deleteProduct($product[ProductInterface::SKU]);
330+
}
331+
306332
/**
307333
* @param array $fixtureProduct
308334
*

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,25 @@ public function testUpdateProductSku()
4949
$updatedProduct->load($productId);
5050
self::assertSame($newSku, $updatedProduct->getSku());
5151
}
52+
53+
/**
54+
* Check Product Repository able to correctly create product without specified type.
55+
*
56+
* @magentoDbIsolation enabled
57+
*/
58+
public function testCreateWithoutSpecifiedType()
59+
{
60+
/** @var Product $product */
61+
$product = Bootstrap::getObjectManager()->get(ProductFactory::class)->create();
62+
$product->setName('Simple without specified type');
63+
$product->setSku('simple_without_specified_type');
64+
$product->setPrice(1.12);
65+
$product->setWeight(1.23);
66+
$product->setAttributeSetId(4);
67+
$product = $this->productRepository->save($product);
68+
69+
self::assertSame('1.1200', $product->getPrice());
70+
self::assertSame('1.2300', $product->getWeight());
71+
self::assertSame('simple', $product->getTypeId());
72+
}
5273
}

0 commit comments

Comments
 (0)