Need to Update Product Media Gallery Image Label
-
09-10-2020 - |
Domanda
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
Soluzione
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();
Altri suggerimenti
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
$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();
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange