Showing results for 
Search instead for 
Did you mean: 

Updating gallery image labels

Updating gallery image labels

$_products = Mage::getModel('catalog/product')
foreach ($_products as $_product) {
    $attributes = $_product->getTypeInstance(true)->getSetAttributes($_product);
    if (!isset($attributes['media_gallery'])) echo 'No Media Gallery Found';

    $images = $_product->getMediaGalleryImages();
    $gallery = $attributes['media_gallery']->getBackend();

    foreach ($images as $image) {
        if (strlen($image->getLabel()) > 0) continue;

        $gallery->updateImage($_product, $image->getFile(), array('label' => $_product->getName()));
        $count++; if ($count > 10) break;

    $_product->setData('media_gallery', $gallery);

// Also tried
//$_product->getResource()->saveAttribute($_product, 'media_gallery');
// Also tried to just $image->setLabel('label') inside the loop


Gets the label just fine, but never saves. For about 1200 items, this code takes 11 minutes so limited to test the first 10.


This script init's using the admin store, and our main store is '1'.


If can't get an answer, gonna just start using sql.


Re: Updating gallery image labels

                $gallery               = $product->getData("media_gallery");
                // get image just added
                $lastImage             = array_pop($gallery["images"]);
                $lastImage["position"] = $position;
                // re-add that image
                array_push($gallery["images"], $lastImage);
                $product->setData("media_gallery", $gallery);

Here is the code for seting the position, maybe this can help