How can I set main product image as thumbnail image programmatically in Magento 2?
Scenario:
I have a loop running trough all products and each product has a main image, only some products have not defined an image for thumbnail. So I want to set the main image also as the product thumbnail for each product.
-
You should take a look here: magento.stackexchange.com/questions/129869/…Khoa Truong– Khoa Truong2016年08月06日 02:56:04 +00:00Commented Aug 6, 2016 at 2:56
2 Answers 2
Thank you for the reply.
I already found the answer by myself and I will share it below.
foreach ($products as $product){
$smallImage = $product->getSmallImage();
$imagePath = '/catalog/product' . $product->getImage();
if( $smallImage == 'no_selection' ){
echo 'FIXING ' . $imagePath . "\r\n";
$product->addImageToMediaGallery( $imagePath, array( 'small_image' ), false, false );
$product->save();
}
}
But still, I'm curious to know what are the Boolean variables are here.
$product->addImageToMediaGallery( $imagePath, array( 'small_image' ), false, false );
-
2The first is $move - if true, it will move source file. The second is $exclude - if set true, mark image as disabled in product page viewRobert Egginton– Robert Egginton2016年11月15日 18:25:53 +00:00Commented Nov 15, 2016 at 18:25
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load product object
$mediaAttribute = array ('thumbnail','small_image','image');
$mediaDir = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList')->getPath('media');// Im Magento 2
$mediaDir = Mage::getBaseDir('media');// In Magento 1
//Case 1: When image files are alredy in your server
$fpath = 'product/images/';// path to your file
$count = 0;
$imgArray = array('image1.png','image2.png','image3.png','image4.png');
foreach ($imgArray as $image){
$imgUrl = _save_image( $fpath.$image,$objectManager,$mediaDir );// copies the file from your local storage to your-magento-root-path/pub/media
if ($count == 0){
$product->addImageToMediaGallery( $imgUrl , $mediaAttribute, true, false );
}else {
$product->addImageToMediaGallery( $imgUrl , null, true, false );
}
$count++;
}
function _save_image($img,$objectManager,$mediaDir) {
$imageFilename = basename($img);
$image_type = substr(strrchr($imageFilename,"."),1); //find the image extension
$filename = md5($img . strtotime('now')).'.'.$image_type; //give a new name, you can modify as per your requirement
if (!file_exists($mediaDir)) mkdir($mediaDir, 0777, true);
else chmod($mediaDir, 0777);
$filepath = $mediaDir . '/'. $filename; //path for temp storage folder: pub/media
file_put_contents($filepath, file_get_contents(trim($img))); //store the image from external url to the temp storage folder
return $filepath;
}
In your case set only 'thumbnail' in $mediaAttribute array.Hence array will be $mediaAttribute = array ('thumbnail');
-
Thank you for your reply, I already found an answer and posted it here.nuwaus– nuwaus2016年08月08日 14:58:37 +00:00Commented Aug 8, 2016 at 14:58