0

I am trying to set up a script to import additional images for products programatically, from the var/import/images folder. I am running it on the command line, via PuTTY. The following code works and shows me the product name but once I uncomment the 2 lines starting with $product it fails without showing any error. Error log says

PHP Parse error: syntax error, unexpected ' ' (T_STRING) in /home/owlbarnradesyste/public_html/testcustomer.php on line 28 which is the $product > save(); line.

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    ini_set('memory_limit', '5G');
    error_reporting(E_ALL);
     
    use Magento\Framework\App\Bootstrap;
    require 'app/bootstrap.php';
     
    $bootstrap = Bootstrap::create(BP, $_SERVER);
     
    $objectManager = $bootstrap->getObjectManager();
    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');
     
    try {
            
        $product = $objectManager->get('Magento\Catalog\Model\Product')->load(17);
        echo $product->getName();
       
        //$product->addImageToMediaGallery('var/import/images/111217.jpg', array('image', 'small_image', 'thumbnail'), true, false);
        //$product->save();
        } 
        catch (Exception $e) {
            echo $e->getMessage();
        }   
    
    ?>

2 Answers 2

0

Your $product->addImageToMediaGallery syntax looks correct. You should use full path to image file /var/www/magento2/var/import/blah'

$product->save() is deprecated

Try using the save method of ProductRepositoryInterface (Magento\Catalog\Api\ProductRepositoryInterface)

$this->_productRepository->save($product);
2
  • Thanks - but it's still not working. I'm not sure i'm using the ProductRepositoryInterface correctly. I have added in use Magento\Catalog\Api\ProductRepositoryInterface; and then $productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); further down, and changed the save to $productRepository->save($product); I also tried it your way but didn't work either. I have tried the full path and also $product->addImageToMediaGallery(__DIR__ .'/var/import/images/111217.jpg' and every combination of different paths and productrepository. Am I missing something? Jul 8, 2021 at 16:35
  • 1
    Changed it to $objectManager = $bootstrap->getObjectManager(); $productRepository = $objectManager->get(ProductRepositoryInterface::class); Progress, a new error "Path "/home/sitename/public_html/var/import/images/111217.jpg" cannot be used with directory "/home/sitename/public_html/pub/media/" Jul 8, 2021 at 16:45
1

Final code looks like this now, thanks! Still getting the other error though, but I will mark your answer as correct and post another question. Think it's permissions.

    <?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product; 
use Magento\Framework\App\Bootstrap;

require 'app/bootstrap.php';
 
$bootstrap = Bootstrap::create(BP, $_SERVER); 
$objectManager = $bootstrap->getObjectManager();
$productRepository = $objectManager->get(ProductRepositoryInterface::class);

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeId = $storeManager->getStore()->getId();
$websiteId = $storeManager->getStore($storeId)->getWebsiteId(); 
 
try {
        
    $product = $objectManager->get('Magento\Catalog\Model\Product')->load(17);
    $product->setStoreId($storeId);
    $product->addImageToMediaGallery(__DIR__ .'/var/import/images/111217.jpg', array('image', 'small_image', 'thumbnail'), false, false);
    $productRepository->save($product);
    //$this->_productRepository->save($product);
echo "saved";
    } 
    catch (Exception $e) {
        echo $e->getMessage();
    }

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.