Magento 2.2
I have product with sku banan
I load it by id (for ex: 7777) via product repository
I set sku pineapple
And then trying to save via product repository
https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Model/ProductRepository.php#L565
$existingProduct is loading by sku (which in my case is pineapple) and does not find it.
so $existingProduct is null
$tempProduct = $this->productRepository->getById($entity_id);
$tempProduct->setData('sku', $sku);
$this->productRepository->save($tempProduct);
Are you sure the product exists with the provided $entity_id? Could you confirm this?
yes, I just replace sku for product
Could you provide some more context around this code?
The error indicates that getById() can't find a product.
Please provide the full error message and the surrounding code.
no, the error is in save() method.
try {
$existingProduct = $this->get($product->getSku());
$product->setData(
$this->resourceModel->getLinkField(),
$existingProduct->getData($this->resourceModel->getLinkField())
);
if (!$product->hasData(Product::STATUS)) {
$product->setStatus($existingProduct->getStatus());
}
} catch (NoSuchEntityException $e) {
$existingProduct = null;
}
then you save with a new sku, it loads nothing
Either there is no product with the provided SKU, or $tempProduct is not a valid subclass of \Magento\Catalog\Api\Data\ProductInterface. Note that instead of setData('sku', $sku) it's better practice to use setSku($sku).
If you need further assistance, I think you should take this to StackOverflow, since it does not seem to be a bug in the Magento code, but rather in your own code.
@dbrekelmans you still can't understand the issue.
I have product with sku banan
I load it by id(for ex 7777) via product repository
I set sku pineapple
And then trying to save via product repository
$existingProduct is loading by sku (which in my case is pineapple) and does not find it.
so $existingProduct is null
I understand now.
Try this:
```
$product = $this->productRepository->getById($entity_id);
$product->setSku($sku);
$product->save();
````
Either there is no product with the provided SKU, or $tempProduct is not a valid subclass of
\Magento\Catalog\Api\Data\ProductInterface. Note that instead ofsetData('sku', $sku)it's better practice to usesetSku($sku).
Not just best practice but I simply don't see method setData declared in this interface or its parents. @luckyraul probably setCustomAttribute('sku', 'pineapple') would do the trick but how do you think setData could work?
@dbrekelmans
no such a difference between methods
public function setSku($sku)
{
return $this->setData(self::SKU, $sku);
}
@orlangur this problem is not about setData or setSku - this problem about repository method save.
setData should not be used here as it is not a part of \Magento\Catalog\Api\Data\ProductInterface.
I do agree though that there seems to be missing logic to get product by ID when it was not found by SKU.
I've experienced same issue before. You cannot use getters and setters on SKU because that is not an EAV attribute, it is part of product entity. If you want to change product SKU, the best way to do this is to use a plugin beforeSetSku, that is how I made it work.
@qsolutions-pl I don't see how ProductInterface contract does not allow changing SKU using setter.
@luckyraul Did you manage to save with product with:
$product = $this->productRepository->getById($entity_id);
$product->setSku($sku);
$product->save();
This way you don't save through the repository and you shouldn't receive the error you encountered.
@dbrekelmans issue reporter already mentioned that problem is in repository :) Obviously $product->save(); does the trick, but it is deprecated, you know.
@luckyraul, thank you for your report.
We've created internal ticket(s) MAGETWO-85218 to track progress on the issue.
Hi @luckyraul
We are closing this issue now as fix has already been delivered to the 2.2-develop branch and will be released with the next functional patch for 2.2.
Thanks
This Issue ist Still existing in 2.3.1
The ProductRepository is unable to handle Sku Changes.
How can this set to be fixed ???
Still there is no Fallback for sku changed or sku not found so try load existingProduct by id when id isset....
ProductRepository function save
$existingProduct = $this->get($product->getSku());
Most helpful comment
Either there is no product with the provided SKU, or
$tempProductis not a valid subclass of\Magento\Catalog\Api\Data\ProductInterface. Note that instead ofsetData('sku', $sku)it's better practice to usesetSku($sku).If you need further assistance, I think you should take this to StackOverflow, since it does not seem to be a bug in the Magento code, but rather in your own code.