Magento – Set base image as small and thumbnail

magento2

I have nearly 20k products and all images have been set to base only. If i go into backend and then set each image one by one to thumbnail and small as well, it will take me forever.
Is there any way to do it via programming or database?

Thanks in advance.

Best Answer

Take a look into talbe eav_attribute, you will find out the (image,small_image,thumbnail) attribute. In magento 2.1.8, it take the id (87,88,89) enter image description here

Use this command below:

 UPDATE catalog_product_entity_media_gallery AS mg,
catalog_product_entity_media_gallery_value AS mgv,
catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE  mg.value_id = mgv.value_id
AND mgv.entity_id = ev.entity_id
AND ev.attribute_id IN (87, 88, 89)
AND mgv.position = 1; 

EDIT: If the query doesn't work, It means you have not had the small and thumbnail before, use this code:

use Magento\Catalog\Model\ProductFactory;

protected $_productFactory;

public function __construct(ProductFactory $productFactory){
     $this->_productFactory   = $productFactory;
}

//Change the image
$productCollection = $this->_productFactory->create()->getCollection();

foreach($productCollection as $product) {
    $imagePath = $product->getMediaGalleryImages()->getFirstItem()->getFile();
    if ($imagePath) {
       $this->_objectManager->get('Magento\Catalog\Model\Product\Action')->updateAttributes([0 => $product->getId()], array("thumbnail" => $imagePath,"small_image" => $imagePath), 0);
    }
}
Related Topic