开发者

Magento Set Product Image Label During Import

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

$product->addImageToMediaGallery($imageFile, array('image','thumbnail','small_image'), f开发者_如何学Calse, 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜