Skip to content

Instantly share code, notes, and snippets.

@nflint
Last active March 21, 2016 21:36
Show Gist options
  • Save nflint/48fb038ea0f568112b6b to your computer and use it in GitHub Desktop.
Save nflint/48fb038ea0f568112b6b to your computer and use it in GitHub Desktop.
Magento Sku Cleanup Script
<?php
# Thanks to: http://stackoverflow.com/questions/28350783/how-to-replace-the-sku-number-for-5000-products-in-magento
# Create a CSV File with column "old_sku" in the first column, and "new_sku"in the second column.
include_once './app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$updates_file="sku-cleanup.csv";
$sku_entry=array();
$updates_handle=fopen($updates_file, 'r');
if($updates_handle) {
while($sku_entry=fgetcsv($updates_handle, 1000, ",")) {
$old_sku=$sku_entry[0];
$new_sku=$sku_entry[1];
echo "<br>Updating ".$sku_entry[2].": ".$old_sku." to ".$new_sku." - ";
try {
$get_item = Mage::getModel('catalog/product')->loadByAttribute('sku', $old_sku);
if ($get_item) {
$get_item->setSku($new_sku)->save();
echo "successful";
} else {
echo "item not found";
}
} catch (Exception $e) {
echo "Cannot retrieve products from Magento: ".$e->getMessage()."<br>";
return;
}
}
}
fclose($updates_handle);
?>
@nflint
Copy link
Author

nflint commented Mar 21, 2016

If you add a name column it will allow you to monitor the process. You csv should look something like this:

old-sku,new-sku,name
1234,4321,My Awesome Product

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment