BLOG

Sometimes nerdy and wordy, but mostly helpful articles on website design and development.

How to mass update your Magento SKUs

| 20 Comments | Magento

We recently worked on a project where we needed to update all of the simple product’s SKUs on a pretty large site. It turned out there were just under 1000 SKUs that needed updated. The reason for needing to change all of the SKUs was to make an integration with an external supply chain management software called BlueCherry. In order to easily update BlueCherry with correct stock, we needed a way to match up items. It would be great to just do a mass export and then mass import to update the SKUs, but since Magento uses the SKU as its unique identifier on import/export we had to find an alternative method.

So, instead of going into each simple product individually, I quickly went to the Magento Community forum and tried to find an answer.

The Magento Wiki has a great article on how to use the Magento API to update all of the SKUs. They have also updated the article on how to use the Magento Models (much quicker than the API) to do the exact same thing, which is what we chose to use.

To use their script, simply create a csv file with two items per line with the old SKU and new SKU separated by a comma. The script will iterate through each product and update its SKU. Pretty simple and a huge time saver! Below is the code.

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

$updates_file="/home/public_html/xxx/var/export/sku2sku.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 ".$old_sku." to ".$new_sku." - ";
        try {
            $get_item = Mage::getModel('catalog/product')-&gt;loadByAttribute('sku', $old_sku);
            if ($get_item) {
                $get_item-&gt;setSku($new_sku)-&gt;save();
                echo "successful";
            } else {
                echo "item not found";
            }
        } catch (Exception $e) { 
            echo "Cannot retrieve products from Magento: ".$e-&gt;getMessage()."
";
            return;
        }
    }
}

fclose($updates_handle);

Head over to the Magento Wiki Article for more information.

20 responses to “How to mass update your Magento SKUs

  1. Thanks for the post.. I’m having some issues gettng it to work for me. I’m getting the following:

    Warning: include_once(../app/Mage.php) [function.include-once]: failed to open stream: No such file or directory in /home/vapornin/public_html/sku2sku.php on line 2

    Warning: include_once() [function.include]: Failed opening ‘../app/Mage.php’ for inclusion (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/vapornin/public_html/sku2sku.php on line 2

    Fatal error: Class ‘Mage’ not found in /home/vapornin/public_html/sku2sku.php on line 3

    Any idea? Thanks!

    1. You’ll need to make sure the include statement is pointing to the actual location of your Mage.php file. If you fix that, you should be good.

  2. Could someone tell me on the include statement, what you mean that it is pointing to the actual; ;location of my mage.php. My installation is in the root so myweb.com/app/mage.php.

    1. my example shows magento in the root directory with the script being run from any folder inside the root directory like var.

  3. Thanks it’s very nice and it’s saved lots of time, I did some changes in my code

    error_reporting(E_ALL | E_STRICT);
    define('MAGENTO_ROOT', getcwd());
    $mageFilename = MAGENTO_ROOT . '/app/Mage.php';
    require_once $mageFilename;
    Mage::setIsDeveloperMode(true);
    ini_set('display_errors', 1);
    Mage::app();
    Mage::app()-&gt;setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    $updates_file="/home/magento/htdocs/sku2sku.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 "Updating ".$old_sku." to ".$new_sku." ";
            try {
                $get_item = Mage::getModel('catalog/product')-&gt;loadByAttribute('sku', $old_sku);
                if ($get_item) {
                    $get_item-&gt;setSku($new_sku)-&gt;save();
                    echo "successful";
                } else {
                    echo "item not found";
                }
            } catch (Exception $e) { 
                echo "Cannot retrieve products from Magento: ".$e-&gt;getMessage()."";
                return;
            }
        }
    }
     
    fclose($updates_handle);
  4. Hi,

    I have a quick question, if I use this to change all my SKU’s, will I lose the Simple Product Associations for Configurable Products?

    Thanks

    Simon

  5. Where do I put this script? just upload it as its own page? or is there somewhere special i need to put this code? i cant see any help on the WiKi and i have over 1000 SKUS to change

    1. Andrew – yes you would create a file in the root of your site (or wherever) to run this. Just know that you would have to adjust the script to make sure you load Mage.php

Comments are closed.

Get the Latest

  • Get email updates whenever we post new blog articles.
  • This field is for validation purposes and should be left unchanged.

Recent Articles