SKU bulk edit from CSV in Magento

Magento
// PHP

What it does

Batch update your product SKUs from a two-column CSV file in Magento.

The CSV file should have the old SKU in the first column and the new SKU in the second column, with no headers.

For example:

6930847831857 8016520327439
SHIRT BLACK S 7325018370241
SHIRT RED S 4041214452608
6968281346879 5439192131604
1538005494092 7167316519928

The Code

include_once './app/Mage.php';
Mage::app();

$skuFile="./var/export/skuUpdate.csv";                                                      // The name and location of your CSV file
$skuEntry=array();
$updateHandle=fopen($skuFile, 'r');
if($updateHandle) {                                                                         // If the file is found
    while($skuEntry=fgetcsv($updateHandle, 1000, ",")) {
        $oldSku=$skuEntry[0];                                                               // Store the old SKU as a variable to find it in Magento's database
        $newSku=$skuEntry[1];                                                               // Store the new SKU as a variable to overwrite the old one
        echo "<br>Updating ".$oldSku." to ".$newSku." - ";                                  // Display progress messages
        try {
            $get_item = Mage::getModel('catalog/product')->loadByAttribute('sku', $oldSku); // Find the item in Magento's database
            if ($get_item) {
                $get_item->setSku($newSku)->save();                                         // Save the new SKU
                echo "successful";                                                          // Show success message
            } else {
                echo "item not found";                                                      // Show failure message
            }
        } catch (Exception $e) {
            echo "Cannot retrieve products from Magento: ".$e->getMessage()."<br>";
        return;
        }
    }
}
fclose($updateHandle);

How To Use It

  1. Save the CSV file as skuUpdate.csv and upload it to /var/export/
  2. Create the file with the PHP code above, save it as newSkus.php and upload it to yourdomain.com/newSkus.php
  3. Visit yourdomain.com/newSkus.php in your browser to run the script

Menu