Tag Archives: Script replace old sku to new sku programmatically in magento

Magento update product sku programmatically using csv

Put a skucsv.csv file which containt 2 columns.
1st column containt OLD sku list and 2nd column containt New sku list.
Create a updateProductSku.php file in root and paste below script on this file.





error_reporting(E_ALL);
ini_set('display_errors', 1);
define('MAGENTO_ROOT', getcwd());
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::init();
$row = 1;

if (($handle = fopen("skucsv.csv", "r")) !== FALSE) {

    while (($sku = fgetcsv($handle, 1000, ",")) !== FALSE) {
 // $sku[0] OLD sku list
 // $sku[1] New sku list
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku',
                $sku[0]);
       
        if ($product && $product->getTypeId() == 'simple') {
            $product->setSku($sku[1]);
            $product->save(); 
        }else{            
            echo $sku[0].' Does not found SKU <br />';
        }

        unset($product);       
        $row++;
    }

    fclose($handle);
}