How to mass update your Magento SKUs
| 20 Comments | MagentoWe 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()." "; return; } } } fclose($updates_handle);
Head over to the Magento Wiki Article for more information.
Hi, Trying to update all SKU’s includiing the previous orders. Any ideas how?
Im a little confused at your question. You’d like to update previous orders or SKUs or both?
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!
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.
I found very useful this information and I want to thank you !
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.
What path is the script file to be placed? Thanks.
my example shows magento in the root directory with the script being run from any folder inside the root directory like var.
Thank you. The script is exactly what I needed for Magento 1.6.1.
how to change old sku no to new sku no plz help me
This script is not ideal for changing sku.
Thanks it’s very nice and it’s saved lots of time, I did some changes in my code
Glad you were able to use it and save you time!
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
It shouldn’t as we are not changing that db field.
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
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
Got a syntax error. Can you teach me how to fix this?
Parse error: syntax error, unexpected ‘&’ in your code on line 16
Oh wait, I just found it out that I should replace the gt with -> Thanks. 😀 Your code works handsomely. 😀
What will happen with images? Should I change image names also?