Skip to content

Instantly share code, notes, and snippets.

@gelanivishal
Created February 10, 2017 16:25
Show Gist options
  • Save gelanivishal/de61b564bc5d85f82cc10377e7717ee4 to your computer and use it in GitHub Desktop.
Save gelanivishal/de61b564bc5d85f82cc10377e7717ee4 to your computer and use it in GitHub Desktop.
Assign first media image as base image.
<?php
include 'app/Mage.php';
Mage::app();
$productId = 4299;
//load the product
$product = Mage::getModel('catalog/product')->load($productId);
//get all images
$mediaGallery = $product->getMediaGallery();
//if there are images
if (isset($mediaGallery['images'])){
//loop through the images
foreach ($mediaGallery['images'] as $image){
//set the first image as the base image
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('image'=>$image['file']), 0);
//stop
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment