Hi All, Recently while working on a project. I need to delete products from Magento and I only have a CSV having all the product's SKU. For that I have written a script which reads SKU from CSV file and deleting products.
Below is the code which you can use:
Below is the code which you can use:
<?php
//Path to Magento
require_once('app/Mage.php');
umask(0);
Mage::app();
ini_set('display_errors', 1);
// Register a secure admin environment
Mage::register('isSecureArea', 1);
$file = "remove_product.csv"; // CSV File name with SKU's
$fileData=fopen($file,'r');
if (($handle = fopen("remove_product.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
$sku = $data[$c];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
if($product) {
try {
$product->delete();
echo $productSku . " Deleted<br>";
} catch (Exception $e) {
echo $productSku . " Not Deleted<br>";
}
} else {
echo $productSku . " Not Available<br>";
}
}
}
fclose($handle);
}
Comments
Post a Comment