Bulk update magento products sku

This tutorial explains how to bulk update magento products sku. This script update all the products sku based on the old sku.

Please also check the tutorial which explains how to import magento products and import tier prices.

Step by step explanation.

  • Connect magento database
  • Read CSV data
  • Bulk update magento products sku

Connect magento database

For bulk update magento products sku as the first step connect to the database by including following code.

require_once '../app/Mage.php';
ini_set('display_errors', 1);

$attributeCode = 'color';
// read new option values from CSV
$newOptionValues = readOptions();
// read existing options
$optionsExists = readExistingAttribute( $attributeCode );
//compare with existing options;
$newOptions = cmpOptions( $newOptionValues, $optionsExists );
createAttribute( $newOptions, $attributeCode);

Read CSV data

As the next step read the data from the CSV data.The following code read the CSV data in an array.

 * Get product info from products.csv
function getProducts()
    $arrResult = array();
    $file = 'csv/products.csv';
    $handle = fopen($file, "r");
    $headers_products = false;
    if (empty($handle) === false) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if (!$headers_products) {
                $headers_products[] = $data;
            } else {
                $arrResult[] = $data;
    return $arrResult;

Bulk update magento products sku

The following code $newdatas array contains the data read from the CSV .In the foreach loop iterate through each values and update the sku as shown.

function rewriteSku($newDatas) {
    $i = 1;
    foreach($newDatas as $newData) {
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $newData[1]);
        if($product) { 
            echo  $i++.' '.$product->getName() . ' SKU updated';
            echo "\n";

Please also refer the tutorial which explains how to develop magento admin module and create custom tables magento module.

Share this Tutorial

2 comments on “Bulk update magento products sku

Leave a Reply

Your email address will not be published. Required fields are marked *