Skip to content

Instantly share code, notes, and snippets.

@ajankovic
Created November 14, 2012 17:39
Show Gist options
  • Select an option

  • Save ajankovic/4073569 to your computer and use it in GitHub Desktop.

Select an option

Save ajankovic/4073569 to your computer and use it in GitHub Desktop.
Assign existing media gallery image to the loaded product.
$product = Mage::getModel('catalog/product')
->setStoreId($store->getId())
->load($product->getId());
$gallery = $product->getMediaGallery();
$mediaAttribute = array('image', 'small_image', 'thumbnail');
if($gallery && count($gallery['images'])) {
$product_attributes = $product->getTypeInstance()->getSetAttributes();
$gal_backend = $product_attributes['media_gallery']->getBackend();
foreach($gallery['images'] as $image_data) {
$gal_backend->setMediaAttribute($product, $mediaAttribute, $image_data['file']);
}
}
$product->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment