i want to programmatically import a product into my magento. Unfortunatly I get an error which is not telling me a lot maybe some of you have an idea. Here is my php code:
echo '<pre>';
echo 'Create Product Model';
echo '</pre>';
$product = $this->productFactory->create();
$product->setSku('my-sku2');
$product->setName('Sample Simple Product');
$product->setAttributeSetId(4);
$product->setStatus(1);
$product->setWeight(10);
$product->setVisibility(4);
$product->setTaxClassId(0);
$product->setTypeId('simple');
$product->setPrice(100);
$product->setStockData(
array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => 1,
'qty' => 999999999
)
);
try {
$this->productRepository->save($product);
echo 'PRODUCT SAVED ';
return 'Saved';
} catch (CouldNotSaveException $e) {
echo 'ERROR COULD';
print_r($e);
} catch (InputException $e) {
echo 'ERROR INPUT';
print_r($e);
} catch (StateException $e) {
echo 'ERROR STATE';
print_r($e);
} catch (LocalizedException $e) {
echo 'ERROR LocalizedException';
echo 'Something failed for product import ' . $product . PHP_EOL;
print_r($e);
}
The error that i get is
1 exception(s):
Exception #0 (Exception): Recoverable Error: Object of class Magento\Catalog\Model\Product\Interceptor could not be converted to string in /Applications/MAMP/htdocs/magento2/app/code/Inchoo/Helloworld/Block/Helloworld.php on line 241
Exception #0 (Exception): Recoverable Error: Object of class Magento\Catalog\Model\Product\Interceptor could not be converted to string in /Applications/MAMP/htdocs/magento2/app/code/Inchoo/Helloworld/Block/Helloworld.php on line 241
#0 /Applications/MAMP/htdocs/magento2/app/code/Inchoo/Helloworld/Block/Helloworld.php(241): Magento\Framework\App\ErrorHandler->handler(4096, 'Object of class...', '/Applications/M...', 241, Array)
I don't have an idea what i am doing wrong. The first time when i let the code ran it was working.
UPDATE
When i remove the try catch part and just call
$this->productRepository->save($product);
I get an error that 1 exception(s):
Exception #0 (Magento\Framework\Exception\NoSuchEntityException): Product with SKU "my-sku2" does not exist
And of course it does not exist because i want to create a new product. So how can i create a new product?
You are trying to convert the $product object to a string in the last try-catch's echo. I believe that is what your Exception message is pointing at. Try to remove the echo and see what the LocalizedException gives instead.
If you want to log $product data, you should consider using $product->debug(); which returns an array of data without all the object recursion. The function can be used on any object extending \Magento\Framework\DataObject.
https://github.com/magento/magento2/blob/2.2/lib/internal/Magento/Framework/DataObject.php#L468
UPDATE
In addition to above, for the NoSuchEntityException you experienced after removing the try-catch; I tried your code and received following error in admin "Please enter a value less than or equal to 99999999." on quantity, might have something to do with the problem.
If you're calling the create from frontend and your Magento is not in single-store mode, it might also be something fuzzy with the store ID. In that case, set $product->setStoreId(0); to make sure it's saving as it would in admin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With