Monday, October 19, 2015
Magento: Product Bulk Images Deletion
<?php
require_once('../app/Mage.php');
umask(0);
Mage::app('admin');
ini_set('display_errors', 1);
echo 'Delete Imgaes script runing at '.date("Y-m-d h:i:sa").' <br/>';
try{
$csv = new Varien_File_Csv();
$csvfilepath = '../../../../tmp/deleteimages.csv';
$cssfile = fopen($csvfilepath,"r");
$data = $csv->getData($csvfilepath);
//Mage::log('CSV file exsist', null, 'bulkimagesimport.log');
echo 'CSV file exsist <br/>';
$rows = count($data);
for ($j = 1; $j<= $rows; $j++) {
$productSKU = $data[$j][0];
if(!empty($productSKU)){
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
if (!$product){
//Mage::log($productSKU.' SKU doesn`t exsist', null, 'bulkimagesimport.log');
echo $productSKU.' SKU doesn`t exsist <br/>';
}else{
//deleting
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$product = Mage::getModel('catalog/product')->load($product->getId());
$items = $mediaApi->items($product->getId());
$attributes = $product->getTypeInstance()->getSetAttributes();
$gallery = $attributes['media_gallery'];
foreach($items as $item){
if ($gallery->getBackend()->getImage($product, $item['file'])) {
$gallery->getBackend()->removeImage($product, $item['file']);
}
}
$product->save();
echo 'Images deleted for sku '.$productSKU.'<br/>';
}
}
}
fclose($cssfile);
echo 'Deletion Successfully at '.date("Y-m-d h:i:sa").' <br/>';
} catch (Exception $e){
//Mage::log($e->getMessage(), null, 'bulkimagesimport.log');
echo $e->getMessage();
}
require_once('../app/Mage.php');
umask(0);
Mage::app('admin');
ini_set('display_errors', 1);
echo 'Delete Imgaes script runing at '.date("Y-m-d h:i:sa").' <br/>';
try{
$csv = new Varien_File_Csv();
$csvfilepath = '../../../../tmp/deleteimages.csv';
$cssfile = fopen($csvfilepath,"r");
$data = $csv->getData($csvfilepath);
//Mage::log('CSV file exsist', null, 'bulkimagesimport.log');
echo 'CSV file exsist <br/>';
$rows = count($data);
for ($j = 1; $j<= $rows; $j++) {
$productSKU = $data[$j][0];
if(!empty($productSKU)){
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
if (!$product){
//Mage::log($productSKU.' SKU doesn`t exsist', null, 'bulkimagesimport.log');
echo $productSKU.' SKU doesn`t exsist <br/>';
}else{
//deleting
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$product = Mage::getModel('catalog/product')->load($product->getId());
$items = $mediaApi->items($product->getId());
$attributes = $product->getTypeInstance()->getSetAttributes();
$gallery = $attributes['media_gallery'];
foreach($items as $item){
if ($gallery->getBackend()->getImage($product, $item['file'])) {
$gallery->getBackend()->removeImage($product, $item['file']);
}
}
$product->save();
echo 'Images deleted for sku '.$productSKU.'<br/>';
}
}
}
fclose($cssfile);
echo 'Deletion Successfully at '.date("Y-m-d h:i:sa").' <br/>';
} catch (Exception $e){
//Mage::log($e->getMessage(), null, 'bulkimagesimport.log');
echo $e->getMessage();
}
Labels:
Magento
Magento: Import Product Bulk Images
<?php
require_once('../app/Mage.php');
umask(0);
Mage::app('admin');
echo 'Bulk Import Imgaes script runing at '.date("Y-m-d h:i:sa").' <br/>';
try{
$csv = new Varien_File_Csv();
$csvfilepath = '../../../../tmp/importimage.csv';
$imagepath = '../../../../tmp/import/';
$cssfile = fopen($csvfilepath,"r");
$data = $csv->getData($csvfilepath);
//Mage::log('CSV file exsist', null, 'bulkimagesimport.log');
echo 'CSV file exsist <br/>';
$rows = count($data);
for ($j = 1; $j<= $rows; $j++) {
$productSKU = $data[$j][0];
if(!empty($productSKU)){
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
if (!$product){
//Mage::log($productSKU.' SKU doesn`t exsist', null, 'bulkimagesimport.log');
echo $productSKU.' SKU doesn`t exsist <br/>';
}else{
//Mage::log($productSKU.' SKU product loaded', null, 'bulkimagesimport.log');
echo $productSKU.' SKU product loaded <br/>';
$imagesArray = explode('|', $data[$j][1]);
$count = 0;
$mediaAttribute = array (
'image',
'small_image',
'thumbnail',
'reference_image',
'faceimage'
);
foreach ($imagesArray as $image){
$imagefile = $imagepath.trim($image);
if (file_exists($imagefile)) {
if ($count == 0){
$product->addImageToMediaGallery( $imagefile , $mediaAttribute, false, false );
}else{
$product->addImageToMediaGallery( $imagefile , null, false, false );
}
$product->setStoreId(0);
//Mage::log($imagefile.' Image uploaded', null, 'bulkimagesimport.log');
echo $image.' Image uploaded <br/>';
} else {
//Mage::log($imagefile.' Image not found', null, 'bulkimagesimport.log');
echo $image.' Image not found <br/>';
}
$count++;
}
$product->save();
//Mage::log($productSKU.' SKU uploaded successfully', null, 'bulkimagesimport.log');
echo $productSKU.' SKU uploaded successfully <br/>';
}
}
}
fclose($cssfile);
echo 'Uploaded Successfully at '.date("Y-m-d h:i:sa").' <br/>';
} catch (Exception $e){
//Mage::log($e->getMessage(), null, 'bulkimagesimport.log');
echo $e->getMessage();
}
require_once('../app/Mage.php');
umask(0);
Mage::app('admin');
echo 'Bulk Import Imgaes script runing at '.date("Y-m-d h:i:sa").' <br/>';
try{
$csv = new Varien_File_Csv();
$csvfilepath = '../../../../tmp/importimage.csv';
$imagepath = '../../../../tmp/import/';
$cssfile = fopen($csvfilepath,"r");
$data = $csv->getData($csvfilepath);
//Mage::log('CSV file exsist', null, 'bulkimagesimport.log');
echo 'CSV file exsist <br/>';
$rows = count($data);
for ($j = 1; $j<= $rows; $j++) {
$productSKU = $data[$j][0];
if(!empty($productSKU)){
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
if (!$product){
//Mage::log($productSKU.' SKU doesn`t exsist', null, 'bulkimagesimport.log');
echo $productSKU.' SKU doesn`t exsist <br/>';
}else{
//Mage::log($productSKU.' SKU product loaded', null, 'bulkimagesimport.log');
echo $productSKU.' SKU product loaded <br/>';
$imagesArray = explode('|', $data[$j][1]);
$count = 0;
$mediaAttribute = array (
'image',
'small_image',
'thumbnail',
'reference_image',
'faceimage'
);
foreach ($imagesArray as $image){
$imagefile = $imagepath.trim($image);
if (file_exists($imagefile)) {
if ($count == 0){
$product->addImageToMediaGallery( $imagefile , $mediaAttribute, false, false );
}else{
$product->addImageToMediaGallery( $imagefile , null, false, false );
}
$product->setStoreId(0);
//Mage::log($imagefile.' Image uploaded', null, 'bulkimagesimport.log');
echo $image.' Image uploaded <br/>';
} else {
//Mage::log($imagefile.' Image not found', null, 'bulkimagesimport.log');
echo $image.' Image not found <br/>';
}
$count++;
}
$product->save();
//Mage::log($productSKU.' SKU uploaded successfully', null, 'bulkimagesimport.log');
echo $productSKU.' SKU uploaded successfully <br/>';
}
}
}
fclose($cssfile);
echo 'Uploaded Successfully at '.date("Y-m-d h:i:sa").' <br/>';
} catch (Exception $e){
//Mage::log($e->getMessage(), null, 'bulkimagesimport.log');
echo $e->getMessage();
}
Labels:
Magento
Subscribe to:
Comments (Atom)