How to mass update your Magento SKUs

Jan 23, 2011 | | 9

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')->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);

Head over to the Magento Wiki Article for more information.

Find this post helpful? Share it!

Custom Magento Development by triple G interactive

We have in-depth knowledge of Magento, and we\'re well versed in utilizing the Magento API to increase the marketability of the products and/or services offered on your Website. Contact us for your next ecommerce project.

9 Responses to “How to mass update your Magento SKUs”

  1. Toronto 22 February 2011 at 4:12 pm #

    Hi, Trying to update all SKU’s includiing the previous orders. Any ideas how?

    • Gregg 23 February 2011 at 10:32 am #

      Im a little confused at your question. You’d like to update previous orders or SKUs or both?

  2. Ben 24 February 2011 at 5:43 pm #

    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!

    • Gregg 24 February 2011 at 6:25 pm #

      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.

  3. Jason 7 June 2011 at 3:52 am #

    I found very useful this information and I want to thank you !

  4. Tom 28 July 2011 at 11:25 am #

    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.

  5. apexj 17 October 2011 at 7:04 pm #

    What path is the script file to be placed? Thanks.

    • Gregg 26 October 2011 at 10:54 pm #

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

  6. Marius 30 December 2011 at 3:16 pm #

    Thank you. The script is exactly what I needed for Magento 1.6.1.


Leave a Reply