0

I have created a php file to assign the base_image, small_image, Thumbnail_image using below script.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $productRepository = $objectManager->get('Magento\Catalog\Model\ProductRepository'); $csv = "local_alain.csv"; if (!empty($argv) && sizeof($argv) > 1) { $csv = $argv[1]; } if (($handle = fopen($csv, "r")) !== FALSE) { while (($data = fgetcsv($handle, 2000, "\t")) !== FALSE) { $num = count($data); if ($num < 1) { continue;} $sku = explode(',',trim($data[0])); $imagePath = $sku[1]; try { $product = $productRepository->get($sku[0]); $productId = $product->getId(); $product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false); $product->save(); } catch (\Exception $e) { echo $e->getMessage()."</br>"; continue; } } fclose($handle); } 

My csv file( in this csv file i am updating the actual image path of the image which want to be assigned as base_image, small_image, Thumbnail_image, These images are already uploaded to products. This image paths are getting from the product export csv)

Beauty Care Product,/1/6/16-02-08651_1.jpeg Vitamin Products,/1/6/16-02-05659_1.jpeg Sports Nutrition Product,/1/6/16-02-05659_1_1.jpege 

But i am getting an error of

Path "/1/6/16-02-05659_1_1.jpeg" cannot be used with directory "/Applications/MAMP/htdocs/alain-ecom/pub/media/"

How can i fix this issue?

1 Answer 1

0

Here my issue was image path. I have added the image path as /1/6/16-02-05659_1_1.jpeg. But actually it should be from the pub/catalog/product.

So i updated my script as below.

$product->addImageToMediaGallery('catalog/product'.$imagePath, array('image', 'small_image', 'thumbnail'), false, false); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.