2

I am assigning multiple images onto a product in my magento store with the below:

$_product = Mage::getModel('catalog/product')->load($id);

$images_array = array("image1.jpg","image2.jpg","image3.jpg");
$_product->setMediaGallery (array('images'=>array (), 'values'=>array ()));

$i = 1;
foreach($images_array as $img){
    $img_url = $path.$img;
    if(file_exists($img_url)){
        if($i==1){
            $_product->addImageToMediaGallery ($img_url, array ('thumbnail','small_image','image'), false, false);
        }else{
            $_product->addImageToMediaGallery ($img_url, null, false, false);
        }
        $i++;
    }
}

I am missing the ability to set the image label during the addImageToMediaGallery method. Is this possible?

5

You need update label after image save

foreach($product->getData('media_gallery') as $each){

    foreach($each as $image) {
        echo "<pre>";
        print_r($image);
        echo "</pre>";  
        $i++;

        $attributes = $product->getTypeInstance(true)
            ->getSetAttributes($product);
    $attributes['media_gallery']->getBackend()->updateImage($product, $image['file'], $data=array('position'=>$i,'label'=>$image['file']));
    }
}

$product->save();
  • Very nice solution, thanks. Do you also happen to know how to add a label for different storeviews? Like for example I want our English storeview to have an English image label string and our Dutch storeview a Dutch image label. – SuperDicq Feb 15 at 15:34

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.