2

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.

asked Aug 5, 2016 at 15:09
1

2 Answers 2

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 );
answered Aug 8, 2016 at 14:57
1
  • 2
    The first is $move - if true, it will move source file. The second is $exclude - if set true, mark image as disabled in product page view Commented Nov 15, 2016 at 18:25
0
$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');

answered Aug 8, 2016 at 12:40
1
  • Thank you for your reply, I already found an answer and posted it here. Commented Aug 8, 2016 at 14:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.