1

I am trying to update the media gallery image label with product_id. I have tried different different codes but didn't get any chance to update the image label. Is there any way of updating media gallery image label programmatically or using csv file ? I am using magento version 1.9.3.X

My Code

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(1);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::init();



$productId = 142;
//load the product
$product = Mage::getModel('catalog/product')->load($productId);


$i = 1;
foreach($product->getData('media_gallery') as $each){
    foreach($each as $image){
        $i++;
        $attributes = $product->getTypeInstance(true)
            ->getSetAttributes($product);
    $attributes['media_gallery']->getBackend()->updateImage($product, $image['file'], $data=array('postion'=>$i,'label_default'=>'labelnametext'));
    }
}
$product->save();

IF anyone have any idea please share with me.

Thanks

2
  • 1
    tell us what you have tried so far, put a code sample over here May 19, 2017 at 5:56
  • please check my code for single product only
    – Jack
    May 19, 2017 at 6:11

3 Answers 3

3

Try below code.

<?php 

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product_id = 142; //Your Product Id
$product = Mage::getModel('catalog/product')->load($product_id);
$attributes = $product->getTypeInstance(true)->getSetAttributes($product);
$gallery = $attributes['media_gallery'];
$images = $product->getMediaGalleryImages();
foreach ($images as $image) {
    echo "<pre>";
    echo $image->getValueId().' - '.$image->getLabel();
    $backend = $gallery->getBackend();
    if($image->getValueId() == 'value_id of image 1'){
        $backend->updateImage(
            $product,
            $image->getFile(),
            array('label' => 'image-label1')
        );
    }
    if($image->getValueId() == 'value_id of image 2'){
        $backend->updateImage(
            $product,
            $image->getFile(),
            array('label' => 'image-label2')
        );  
    }
}
$product->setMediaGalleryImages($images);
$product->save();
2
  • Yes Thanks its worked. But it saves same label name for each image. I want to update diff -2 label name for each image.
    – Jack
    May 19, 2017 at 7:26
  • You can set the image label for multiple images with the help of value_id. Check my updated code. And if it works, you can hit the Upvote :) May 19, 2017 at 7:48
1

Try below code

I think $product->save() give you fatal error , try to save Attribute please check answer for detail , and also set admin store for saving detail otherwise you get error store not found.

       Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); // set Admin store for your script Otherwise you get Fatal error 
       $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'); //Attribute Save 
4
  • I have tried this one also but didnt update the value. thanks for your reply
    – Jack
    May 19, 2017 at 6:26
  • OK got it your issue give me 10 min I will update the code May 19, 2017 at 6:27
  • @Ch037 please check updated code May 19, 2017 at 6:29
  • sorry its also didnt work for me.
    – Jack
    May 19, 2017 at 6:35
0
 $sku =  "test";   //$value[0]; echo "<br>"; 
 $product =  Mage::getModel('catalog/product')->loadByAttribute('sku', $sku );



            if(is_object($product) )
            {

                  $baseImage                  =  $value[1];
                  $fullBaseImage              = Mage::getBaseDir('media').DS.'import'.DS.'HSD-optimized-images-toupload-1'.DS.$baseImage; 

                  $additionalImage            =  $value[2];
                  $fullAdditionalImage        = Mage::getBaseDir('media').DS.'import'.DS.'HSD-optimized-images-toupload-1'.DS.$additionalImage;


                $attributes = $product->getTypeInstance(true)->getSetAttributes($product);
                $media_gallery = $attributes['media_gallery'];
                $backend = $media_gallery->getBackend();
                $backend->afterLoad($product);
                $count = count($product->getMediaGalleryImages()) + 1; 



                  if($baseImage!='' && is_file($fullBaseImage)  )
                  {
                        $media_attributes = array('image','small_image','thumbnail');
                        $filename = $media_gallery->getBackend()->addImage($product, $fullBaseImage, $media_attributes,false, false);
                        $attributes['media_gallery']->getBackend()->updateImage($product, $filename, array('position' => $count));
                        $product->save();


                  }
                  else if($additionalImage!='' && is_file($fullAdditionalImage)  )
                  {
                        $filename = $media_gallery->getBackend()->addImage($product, $fullAdditionalImage, null,false, false);
                        $attributes['media_gallery']->getBackend()->updateImage($product, $filename, array('position' => $count));
                        $product->save();

                  }


            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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