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!
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With