Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Set Product Image Label During Import

Tags:

magento

I'm trying to import product images to Magento by using

$product->addImageToMediaGallery($imageFile, array('image','thumbnail','small_image'), false, false);

However, I can't figure out a way to set the image's label. I've tried getting the gallery using getMediaGallery , manually set the value and assign it back to the product with setMediaGallery, but it throws an exception.

Does anyone has experience with this? Thanks!

like image 870
clx Avatar asked Aug 27 '11 14:08

clx


2 Answers

After you add the image to the media gallery through this code...

$product->addImageToMediaGallery($imageFile, array('image','thumbnail','small_image'), false, false);

...get the media_gallery array from the product, and then pop the latest image being added, and there you can set the label.

After that you can push it back to the images array of the media_gallery, here's the code:

$gallery = $product->getData('media_gallery');
$lastImage = array_pop($gallery['images']);
$lastImage['label'] = $image_label;
array_push($gallery['images'], $lastImage);
$product->setData('media_gallery', $gallery);
$product->save();
like image 111
Jerico Legaspi Avatar answered Sep 28 '22 18:09

Jerico Legaspi


Had the same task a few days ago, it can be solved by extending core classes (putting them in the 'local' code pool)

in Mage/Catalog/Model/Product.php add new parameter $label='' to method addImageToMediaGallery and pass it to $mediaGalleryAttribute->getBackend()->addImage($this, $file, $mediaAttribute, $move, $exclude, $label);

in Mage/Catalog/Model/Product/Attribute/Backend/Media.php again add new parameter $label='' and change 'label' => '' to 'label' => $label

HTH

like image 35
Zifius Avatar answered Sep 28 '22 17:09

Zifius