Skip to content

Instantly share code, notes, and snippets.

@gibbs
Created June 13, 2014 12:54
Show Gist options
  • Save gibbs/dca0624930c78a3d40a3 to your computer and use it in GitHub Desktop.
Save gibbs/dca0624930c78a3d40a3 to your computer and use it in GitHub Desktop.
Magento - Set all product images to small, base and thumbnail
<?php
require 'app/Mage.php';
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach ($products as $product) {
if (!$product->hasImage()) continue;
if (!$product->hasSmallImage()) $product->setSmallImage($product->getImage());
if (!$product->hasThumbnail()) $product->setThumbnail($product->getImage());
$product->save();
}
@unfeasible
Copy link

This is wonderful!
Is there any way to make it work in groups for bigger stores, to avoid timeouts?

@liangzan
Copy link

I used a variation of this gist that worked on Magento 1.9

I used this to get the products

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->setPageSize(5000);

And this to get the images

$gallery_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages();
$path = $gallery_images->toArray()["items"][0]["file"];

Other than that, the setting of the product images works

if (!$product->hasSmallImage()) $product->setSmallImage($path);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment