Last active
April 15, 2019 04:27
-
-
Save rintoug/1ea189a5104862cdba92ea63463bf572 to your computer and use it in GitHub Desktop.
Magento1 - Remove existing Media Gallery and Add New One at same loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$product = Mage::getModel ( 'catalog/product' ); | |
$product_id = $product->getIdBySku ( $sku); | |
$product->load ( $product_id ); | |
/** | |
* BEGIN REMOVE EXISTING MEDIA GALLERY | |
*/ | |
$attributes = $product->getTypeInstance ()->getSetAttributes (); | |
if (isset ( $attributes ['media_gallery'] )) { | |
$gallery = $attributes ['media_gallery']; | |
//Get the images | |
$galleryData = $product->getMediaGallery (); | |
foreach ( $galleryData ['images'] as $image ) { | |
//If image exists | |
if ($gallery->getBackend ()->getImage ( $product, $image ['file'] )) { | |
$gallery->getBackend ()->removeImage ( $product, $image ['file'] ); | |
} | |
} | |
$product->save (); | |
} | |
/** | |
* END REMOVE EXISTING MEDIA GALLERY | |
*/ | |
try { | |
$galleryData = explode ( ';', $value ); | |
foreach ( $galleryData as $gallery_img ) /** | |
* @param directory where import image reides | |
* @param leave 'null' so that it isn't imported as thumbnail, base, or small | |
* @param false = the image is copied, not moved from the import directory to it's new location | |
* @param false = not excluded from the front end gallery | |
*/ | |
{ | |
$product->setMediaGallery ( array ('images' => array (), 'values' => array () ) ); | |
if (file_exists ( Mage::getBaseDir ( 'media' ) . DS . 'import' . $gallery_img )) { | |
$product->addImageToMediaGallery ( Mage::getBaseDir ( 'media' ) . DS . 'import' . $gallery_img, array ("thumbnail", "small_image", "image" ), false, false )->save (); | |
} | |
} | |
} catch ( Exception $e ) { | |
echo $e->getMessage (); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment