I want to copy the images of a product to another product. I wrote the following script:
$mediaGallery = $anotherProduct->getMediaGallery();
$p->setMediaGallery(null);
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
foreach($mediaGallery["images"] as $image) {
$filePath = $importDir.$image["file"];
if(file_exists($filePath) && !file_exists(Mage::getBaseDir('media').DS.'import'.DS.'catalog'.DS.'product'.$image["file"])) {
$p->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->addImageToMediaGallery($filePath, array('small_image','thumbnail','image'), false, false)->save();
$attributes = $p->getTypeInstance(true)->getSetAttributes($p);
$attributes['media_gallery']->getBackend()->updateImage($p, $image["file"], array('postion'=>$image['postion'],'postion_default'=>$image['postion_default'],'label'=>$image['label'],'label_default'=>$image['label_default'],'disabled'=>$image['disabled'],'disabled_default'=>$image['disabled_default']));
}
}
There is exact one image in $mediaGallery
.
The Script imports two entries (one is empty). The other entry is correct but the labels (for each store) are missing.
Any idea?
$mediaGallery["images"]
array to see if there is an empty one in there? If yes, just do aif($image["file"] !='')
after you start your foreach loop. – tecjam Oct 26 '15 at 15:55