1

i want to set the product image from my folder.For that i am doing like this but no luck.

$image =  Mage::getBaseDir('media') . DS ."users/a.jpg";
$product = Mage::getModel('catalog/product')->load($product_id);
$product->setSmallImage($image)
    ->setThumbnail($image)
    ->setImage($image)
    ->save();

Also try like this :

if(is_file($image))
{
    $product->addImageToMediaGallery($image, array ('image', 'small_image', 'thumbnail'), false, false);
}

What i miss ? thanks for the help

0

I came across the same situation some days ago and this is what i did.

$fileName = $importData['image'];
    $filePath = $importDir . $fileName;
    $base = $importData["base_image"];
    $small = $importData["small_image"];
    $thumbnail = $importData["thumbnail"];

    $productId = $product->loadByAttribute('product_code', $importData['sku']);

    if ($productId) {

        $ourProduct->addImageToMediaGallery($filePath, array("thumbnail","small_image","image"), false, false);
    }

    try {
        $product->save();

    } catch (Exception $e) {
        Mage::log($e->getMessage(), null, 'mylogfile.log');
    }

I used it to import multiple images at once using a csv file. Just replace the values $base,$small, $thumbnail with your images and you are good to go.

In your case that would be:

$image =  Mage::getBaseDir('media') . DS ."users".DS."a.jpg";
$product = Mage::getModel('catalog/product')->load($product_id);
$product->addImageToMediaGallery($image, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();

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.