magento2: 2.4 : When saving a product using the product repository, the price is not saved
I’m trying to programmatically create a product, but upon the first save, the price is not saved, thus returning ‘NULL’. On a second save (with the identical object), the price gets saved.
Preconditions
- Magento 2.4-develop
- php v 7.2
Steps to reproduce
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productFactory->create();
$product->setSku('abc');
$product->setName('Name');
$product->setPrice(9.95);
$product->setAttributeSetId(4);
// Save once, the price does not get saved:
$this->productRepository->save($product);
// Check if the product is saved:
$product = $this->productRepository->get('abc');
$this->assertEquals('abc', $product->getSku()); // passes
$this->assertEquals('Name', $product->getName()); // passes
$this->assertEquals(9.95, $product->getPrice()); // fails (null does not equal 9.95
When I add an extra save()
-instruction it works:
// Save twice, the price gets saved:
$this->productRepository->save($product);
$this->productRepository->save($product);
Expected result
The price should be saved in the first run
Actual result
The price only gets saved the second time the save()
-method is executed
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 10
- Comments: 16 (8 by maintainers)
There is an easy fix 😃
When creating product, assign the type id and price magically appears!
I’ve found another strange behavior with this:
Upon the second save, the
catalog_product_entity_xxx
-tables are populated with duplicate values for every store view. So to recap:0
, but does not save the price.Even if I explicitly use
setStoreId(0)
, the (duplicate) values get saved for all store views.