0
$product1 = Mage::getModel('catalog/product')->loadByAttribute('sku', 'FN244403');
$product1ID = $product1->getId();
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "Select * from `catalog_product_entity_media_gallery` WHERE `entity_id` = '$product1ID' 

LIMIT 1";
$rows       = $connection->fetchAll($sql);
echo $path = $rows[0]['value'];

So, how i can now set Base, Small image, etc? I am try do it, but this not help

try {
    $product1->setSmallImage($path)
        ->setThumbnail($path)
        ->setImage($path)
        ->save();
} catch (Exception $e) {
    echo $e->getMessage();
}
1

As long as $path is an absolute, filesystem URL, then you can run the following:

$product1->addImageToMediaGallery($path, array( 'image', 'small_image', 'thumbnail' ), false, false);

The array seen here sets the image to be the main image, small image and thumbnail. Just ommit those you don't need.

Function reference here: http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Model_Product.html#addImageToMediaGallery describes that the function takes the following parameters:

void addImageToMediaGallery (string $file, [string|array $mediaAttribute = null], [boolean $move = false], [boolean $exclude = true]) 

string $file: file path of image in file system 
string|array $mediaAttribute: code of attribute with type 'media_image', leave blank if image should be only in gallery 
boolean $move: if true, it will move source file    
boolean $exclude: mark image as disabled in product page view   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.