2

I want to upload a product along with product image, please suggest how to set path or upload image for a product. here is my code

 try{
                $_product->setName($product["name"]);
                $_product->setTypeId('simple');
                $_product->setAttributeSetId(4);
                $_product->setSku($product["sku"]);
                $_product->setWebsiteIds(array(1));
                $_product->setVisibility(4);
                $_product->setDescription($product["description"]);
                $_product->setPrice($product["price"]);

                $tmpDir = $this->getMediaDirTmpDir();
                $this->file->checkAndCreateFolder($tmpDir);
                $newFileName = $tmpDir . baseName($imageUrl);
                $result = $this->file->read($imageUrl, $newFileName);
                 if ($result) {
                /** add saved file to the $product gallery */
                $_product->setMediaGallery(array('images'=>array (),'values'=>array ())); 
                $_product->addImageToMediaGallery(how to set path, what to do,array('image', 'small_image', 'thumbnail'), true, $visible);
                }

         'manage_stock' => 1, //manage stock
   'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
                        'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
                        'is_in_stock' => $product['stockstatus'], //Stock Availability
                        'qty' =>  $product['quantity']
                        )
                    );
                $_product->save();

            }                             
            catch(\Exception $e) { 
                 echo $e->getMessage();
            }
2
  • i am suggesting you please try this in test script from root folder and hard code the image path first, if it works then do it dynamically next stackoverflow.com/questions/40259303/… Commented Aug 14, 2017 at 9:36
  • i did but always got image does not exist this error
    – Jay Kapoor
    Commented Aug 14, 2017 at 9:44

3 Answers 3

0

Simply add this code after product price (No need another code for upload image)

 $imagePath = "D:\image\sample.jpg"; // path of the image
 $_product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), true, false);
 $_product->save();
2
  • is the same path of all the words system ?
    – HaFiz Umer
    Commented Jul 15, 2019 at 12:27
  • no luck @israt :( Commented Dec 28, 2020 at 14:59
0
 use Magento\Framework\App\Bootstrap;
 require __DIR__ . '/app/bootstrap.php';

 $bootstrap = Bootstrap::create(BP, $_SERVER);

 $obj = $bootstrap->getObjectManager();

 $state = $obj->get('Magento\Framework\App\State');
 $state->setAreaCode('frontend');

 try
 {
  
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $importDir = __DIR__ . '/pub/media/catalog/product'; //This is the directory path from where you have to take the images
  
  $i = '12'; // It must be product ID for which product it is to be assigned
  
  $product = $objectManager->get('Magento\Catalog\Model\Product')->load($i);
   
  $id = $product->getId();
  $url = $importDir . $product->getImage();
  $product->addImageToMediaGallery($url, array('image', 'small_image', 'thumbnail'), true, false);
  $product->save();
  echo "<br /><br /> $id Product Save Succefully";
 
 }
 catch(\Exception $e)
 {
  echo $e->getMessage();
  exit;
 }
0
foreach ($productImages as $key => $productImage) {
    if ($key === array_key_first($productImages)) {
        $product->addImageToMediaGallery($productImage, array('image', 'small_image', 'thumbnail'), true, false);
        continue;
    }
    $product->addImageToMediaGallery($productImage, [], true, false);
}
$product->save();

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.