Magento establece la etiqueta de imagen del producto durante la importación

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!

preguntado el 27 de agosto de 11 a las 14:08

3 Respuestas

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();

Respondido 11 Oct 12, 01:10

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='' al método addImageToMediaGallery y pasarlo a $mediaGalleryAttribute->getBackend()->addImage($this, $file, $mediaAttribute, $move, $exclude, $label);

in Mage/Catalog/Model/Product/Attribute/Backend/Media.php again add new parameter $label='' y cambio 'label' => '' a 'label' => $label

HTH

Respondido 03 Abr '16, 23:04

I did as you explained, then checked my application were using the right classes (all right), but even if i change 'label'=>'mylabel' in function call, this does not work in Magento 1.6.0. - Dmitri Sologoubenko

@D.S. That is strange, I have tested this code on EE 1.10 and it worked perfectly. I don't think it differs in this regard between editions, but perhaps CE has another method for saving labels? Did you check with xdebug? I couldn't check it right now though - Zifius

@Zifus No, I've only printed currently used class names in order to assure my inherited classes are used instead of the Magento defaults... Thank you anyway for showing me how to quickly override default Magento behaviour :) - Dmitri Sologoubenko

@D.S. Main thing that I've missed is that your import adapter Mage_Catalog_Model_Convert_Adapter_Product must pass a label to addImageToMediaGallery método - Zifius

This is the most elegant solution. Why this isn't already implemented this way already in the core files in beyond me... - Hubert Perron

I recomend you to give a try to Magmi, you can assign labels and it is super fast.

Respondido 27 ago 11, 22:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.