3

I added the code to update the product images programmatically.

Please refer the below code

$firstProduct  = Mage::getModel ('catalog/product')                                 
                        ->load($firstProductId);

$firstProduct->setMediaGallery (array('images'=>array (), 'values'=>array ()))
                    ->addImageToMediaGallery(
                        $filePath,
                        array('image','thumbnail'),
                        false,
                        false
                    );
$firstProduct->save();

/*debug*/
$mediaGallery = $firstProduct->getMediaGallery();
print_r($mediaGallery);

My doubt here is, how to set added image as base image.

Thank you for your valuable inputs

3 Answers 3

4

To save product base image use this,

if(//baseimage only one time)
{
    $mediaAttribute = array (
                'image',
                'thumbnail',
                'small_image'                  
            );
}
else
{
    $mediaAttribute = null;
}

$product->addImageToMediaGallery($filepath_to_image, $mediaAttribute, true, false);

Edited

//3rd param is move, if it is true then move source file
//4th param is exclude,if true mark image as disabled in product page view 

if you create a new product then, setting it with second attribute array as 'image','thumbnail','small_image' automatically set as base image, or if you edit your product, then just save new image as base with first condition i given, it sets default base image.

8
  • Thanks for your reply. I tried but no luck, i checked function with core file 3 parameter stands for moving source file. Please let me know if i am doing wrong.
    – Venkat
    Nov 21, 2014 at 11:03
  • What you have tried and what you get with my code.It should be used to set for only one image.Try my edited code. Nov 21, 2014 at 11:17
  • I tried the same code above, images are added. But in admin base, small and thumbnail images radio buttons are not checked for that image. Regarding third parameter i set as false because i don't want to move the original image file.
    – Venkat
    Nov 21, 2014 at 11:38
  • @Venkat so you are editing the product right? Nov 21, 2014 at 11:39
  • yes, in existing product adding new image and make this is base image.
    – Venkat
    Nov 21, 2014 at 11:41
1

I've got this working for Magento 1.9

<?php
require 'app/Mage.php'; 
Mage::app();

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->setPageSize(5000);
foreach ($products as $product) {
  $gallery_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages();
  $path = $gallery_images->toArray()["items"][0]["file"];
  if (!$product->hasImage()) continue;
  if (!$product->hasSmallImage()) $product->setSmallImage($path);
  if (!$product->hasThumbnail()) $product->setThumbnail($path);
  $product->save();
}
?>
1

Please use below code for assign base image for a product if not assign.

<?php
set_time_limit(0);
include_once "app/Mage.php";
Mage::init();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$app = Mage::app();

$products = Mage::getResourceModel('catalog/product_collection');
$c = 0;
foreach($products as $p){
    $pid = $p->getId();
    $product = Mage::getModel('catalog/product')->load($pid);

    //get all images
    $mediaGallery = $product->getMediaGallery();

    //if($product->getImage() != "no_selection" ){   OR

    if($product->getImage()){
        continue;
    }else{
        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'],'small_image'=>$image['file'],'thumbnail'=>$image['file']), 0);

                $c++;
                break;
            }
        }
    }
}
echo($c . " product(s) updated.");
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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