6

I'm trying to update my product images' labels. I want them to be the same as the name of the product.

What I've tried

$mediaModel = Mage::getModel("catalog/product_attribute_backend_media");
$images = $product->getMediaGalleryImages();
foreach ($images as $image) {
    $mediaModel->updateImage(
        $product->getId(),
        $image->getFile(),
        array("label" => $title)
    );
}

But then I get this error

Fatal error: Call to a member function getAttributeCode() on a non-object in app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php on line 401

I've also tried

$mediaModel = Mage::getModel("catalog/product_attribute_media_api");
$images = $mediaModel->items($product->getId());
foreach ($images as $image) {    
    $mediaModel->update(
        $product->getId(),
        $image['file'],
        array("label" => $title)
    );
}

Which runs fine, but the values aren't updated in the admin.

How do I go about this?

  • If this isn't working then you can try run query against database catalog_product_entity_media_gallery catalog_product_entity_media_gallery_value these look into these two tables – Mufaddal May 22 '13 at 5:19
  • I was able to get the second method working. My problem was I was looping through several products and need to call $product->reset() – Steve Robbins May 22 '13 at 17:58
8

The following code should work for you.

$product = mage::getModel('catalog/product')->load(16);
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);
$gallery = $attributes['media_gallery'];
$images = $product->getMediaGalleryImages();
foreach ($images as $image) {
    $backend = $gallery->getBackend();
    $backend->updateImage(
        $product,
        $image->getFile(),
        array('label' => 'Blah')
    );
}
$product->getResource()->saveAttribute($product, 'media_gallery');

I was getting an error on simply $product->save(); but saving the one attribute seems to work.

  • Hello David, Thanks for this amazing code. But this code works only for default store. I want to add gallery labels for multiple store view wise. So can you please tell me how can I do that? – Indian Sep 7 '15 at 6:17
  • In my case it is not changing the label of the image. I am using Magento 1.9.0.1. – Abbas Mar 21 '18 at 2:36
2

The above code didn't work for me. Here is the changed code that end up working:

function _SetProductImagesAlt($product, $altText='') {
  if (is_numeric($product))
  {
    $product = mage::getModel('catalog/product')->load($product);
  }
  else //reload the product
  {
    $product = mage::getModel('catalog/product')->load($product->getId());
  }

  //set alt to the name of the Product if not set otherwise
  if (empty($altText))
  {
    $altText = $product->getName();
  }

  //sanitize string
  $altText = str_replace("\r", "", $altText);
  $altText = str_replace("\n", "", $altText);

  $altText = htmlspecialchars($altText, ENT_QUOTES, 'UTF-8');

  $attributes = $product->getTypeInstance(true)->getSetAttributes($product);
  $images = $product->getMediaGalleryImages();
  $mediaGalleryBackendModel = $attributes['media_gallery']->getBackend();
  foreach ($images as $image) {
    $mediaGalleryBackendModel->updateImage($product, $image->getFile(), array('label' => $altText));
  }
  $product->save();   
}

Note: the reloading of the product object in the line 8 turned out to be important if you used the same object to add a new product beforehand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.