skip to Main Content

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 MagentoCatalogModelProductInterceptor 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 MagentoCatalogModelProductInterceptor 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): MagentoFrameworkAppErrorHandler->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 (MagentoFrameworkExceptionNoSuchEntityException): 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?

2

Answers


  1. If you are using productFactory to create the product model, why aren’t you just saving it?

    $product->save();
    
    Login or Signup to reply.
  2. 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 MagentoFrameworkDataObject.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search