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.
/** @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);
The price should be saved in the first run
The price only gets saved the second time the save()-method is executed
@kanduvisla, thank you for your report.
We've acknowledged the issue and added to our backlog.
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.
We are working on it #MLAU18
We are working on it #MLAU18
I have investigated this issue but unfortunately this can not be fixed easily.
Although the symptoms is the same (ProductRepository doesn't save price attribute on creating new product), there are 2 different causes depending on the area which the code was called from.
First, if you are calling the above code snippet from an area different than adminhtml (frontend, global, crontab ...), product type does not default to simple, hence when the Resource class tries to save the product it doesn't load the product type attribute set and strips out a whole heaps of attributes, price is one of them. This can be fixed but I will create a separate issue.
In short, this code will work as a work around for now:
$this->state->setAreaCode('global'); // or frontend / crontab...
$product = $this->productFactory->create();
$product->setSku('abcd');
$product->setName('abcd');
$product->setPrice(9.95);
$product->setAttributeSetId(10);
$product->setTypeId('simple');
$this->productRepository->save($product);
$product = $this->productRepository->get('abcd');
Now, if you are calling the above code in a setup script and set the area as adminhtml. There is an observer that check if the current admin user has permission to create new product with price:
// Magento\PricePermissions\Observer\CatalogProductSaveBeforeObserver
if ($product->isObjectNew() && !$this->observerData->isCanReadProductPrice()) {
$product->setPrice($this->observerData->getDefaultProductPriceString());
}
Since we are calling this from a setup script, there is not admin user, hence the ACL check will fail and the product's price attribute is stripped out.
I think the correct fix for this would be to create another area for setup script and refactor the code involved in ProductRepository to consider the new area, but this seems to be a very big changes.
Moreover it would be best to prevent product creation from certain area such as frontend.
@okorshenko @magento-engcom-team can you please provide some suggestions.
@willtran : for full disclosure: I'm trying to create a product while in emulated global area code:
$this->state->emulateAreaCode(Area::AREA_GLOBAL, [$this, 'createProduct'], [$product]);
There is an easy fix :)
When creating product, assign the type id and price magically appears!
$product->setTypeId('simple')
//->setStoreId(0)
->setName($name)
->setSku($name)
->setUrlKey($name)
// ->setVisibility(Visibility::VISIBILITY_BOTH)
// ->setStatus(Status::STATUS_ENABLED)
->setPrice(4)
->setAttributeSetId(4)
->save();
I am working on this at #dmcdindia19
@viladimir thank you for joining. Please accept team invitation here and self-assign the issue.
Same issue on Magento 2.3.3 when setting product image, small_image and thumbnail
$product->setData('image', $image_path);
$product->setData('small_image', $image_path);
$product->setData('thumbnail', $image_path);
I have to save it twice to properly work!
$product->save(); This is use to save pictures because using productRepository I got exception "Invalid image provide" after using the product save method I have to use the productRepository object to save image, small_image, thumbnail attributes
$this->productRepository->save($product);
Hi @engcom-Alfa. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
Hi @chohanbhagchand. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
:white_check_mark: Confirmed by @engcom-Alfa
Thank you for verifying the issue. Based on the provided information internal tickets MC-30625 were created
Issue Available: @engcom-Alfa, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._
Most helpful comment
There is an easy fix :)
When creating product, assign the type id and price magically appears!