1

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.

1
  • Its Magento 2.2.1 site.
    – Usman
    Commented Dec 19, 2017 at 1:56

1 Answer 1

5

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);
    }
}
10
  • It is for magento 1 i think, didn't work for me. Error: SQL query: 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 mg.entity_id = ev.entity_id AND ev.attribute_id IN (87, 88, 89) AND mgv.position = 1 MySQL said: Documentation #1054 - Unknown column 'mg.entity_id' in 'where clause'
    – Usman
    Commented Dec 19, 2017 at 2:21
  • Ah, change the mg.entity to mgv.entity. Don't forget to backup database firrst
    – Nero Phung
    Commented Dec 19, 2017 at 2:44
  • Query ran successfully, what i found it set the base image as small too but not thumbnail.
    – Usman
    Commented Dec 19, 2017 at 3:42
  • I think it did not work, because the product i created for this purpose to test has only base, it didn't work.
    – Usman
    Commented Dec 19, 2017 at 3:49
  • I have updated my answer
    – Nero Phung
    Commented Dec 19, 2017 at 3:50

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.