I\'m having a problem with a third-party ioncube loader encoded feedloader plugin which is no longer supported by the original
$productId = 1;
//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;
}
}
This is the solution I eventually used in 'shell/fix_images.php':
<?php
ini_set('display_errors','On');
ini_set('memory_limit','512M');
error_reporting(E_ALL);
require_once('abstract.php');
class Mage_Shell_Updater extends Mage_Shell_Abstract
{
public function run()
{
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('is_imported', 1); // attribute added by importer
$c=0;
foreach($products as $p) {
$pid = $p->getId();
$product = Mage::getModel('catalog/product')->load($pid);
$mediaGallery = $product->getMediaGallery();
if (isset($mediaGallery['images'])){
foreach ($mediaGallery['images'] as $image){
Mage::getSingleton('catalog/product_action')
->updateAttributes(array($pid), array('image'=>$image['file']), 0);
$c++;
break;
}
}
}
echo($c . " product(s) updated.");
}
}
$shell = new Mage_Shell_Updater();
$shell->run();
If anyone should use this, be sure to remove the 'addAttributeToFilter' from your own Mage method chain.. If you are going to run this as a standalone script (without disabling the realtime indexing first), add this code at the beginning of run():
$pCollection = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($pCollection as $process) {
$process->setMode(Mage_Index_Model_Process::MODE_MANUAL)->save();
}
at the end of run():
foreach ($pCollection as $process) {
$process->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)->save();
}
Also, the abstract.php is from Mage_Shell, usually located in the /shell/ directory in the root of your Magento installation.
In my problem i have set the images for the enabled grouped product. I have set the last image as the base,small and thumbnail image.
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter("type_id",array("eq","grouped"))
->addAttributeToFilter("status",array("eq","1"));
foreach($collection as $tmpProduct) {
$sku = $tmpProduct->getSku();
$product = Mage::getModel('catalog/product')
->loadByAttribute("sku",$sku);
$_images = Mage::getModel('catalog/product')
->load($product->getId())
->getMediaGalleryImages();
$checkImage = Mage::getBaseDir('media').DS.'catalog'.DS.'product'.DS.$product->getImage();
if( $product->getImage()!='' && is_file($checkImage)) {
continue; // do nothing
} else {
if($_images) {
$baseImage = '';
foreach($_images as $_image) {
$baseImage = $_image->getFile();
}
$groupedImagePath = Mage::getBaseDir('media').DS.'catalog'.DS.'product'.DS.$baseImage;
$product->setMediaGallery(
array('images'=>array (),
'values'=>array ()));
$product->addImageToMediaGallery(
$groupedImagePath,
array('image', 'thumbnail', 'small_image'),
false,
false);
Mage::getModel('catalog/product_attribute_media_api')
->remove($product->getId(),$baseImage);
$product->save();
echo "<br>$sku has selected image <br>";
} } }