Magento Set Product Image Label during import

I am trying to import product images in Magento using

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

However, I cannot determine how to set the image label. I tried to get the gallery using getMediaGallery , manually setting the value and assigning it back using setMediaGallery , but it throws an exception.

Does anyone have any experience? Thanks!

+6
source share
3 answers

After adding the image to the media gallery using this code ...

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

... get the media_gallery array from the product, and then add the last added image and there you can set the label.

After that, you can return it back to the media_gallery image media_gallery , here is 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(); 
+8
source

Having the same task a few days ago, it can be solved by expanding the main classes (placing them in the "local" pool of codes)

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

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

NTN

+3
source

I recommend you try Magmi , you can assign tags, and it is very fast.

0
source

Source: https://habr.com/ru/post/896038/


All Articles