Update Magneto Product Skus En Masse

I was recently presented with a challenge to update hundreds of product skus.  It was going to be quite a challenge to do.  We didn’t want to go in and just change a bunch of skus one by one, as that would’ve taken way too long.  So I did some research.  I found this article on Magento’s website:

Update SKUs with dataflow en-masse from a .csv file

The script did not work.

After some tinkering, I saw you had to set the store before you can make certain changes to products.  I added that and Eureka!  It worked!

So, for those of you who wish to update their skus in bulk, enjoy:


< ?php

include 'app/Mage.php';
umask ( 0 );
Mage::setIsDeveloperMode ( true );
Mage::app ();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

ini_set ( 'display_errors', 1 );

$updates_file = getcwd () . "/path/to/your/sku/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' )->load(Mage::getModel('catalog/product')->getIdBySku($old_sku))->setSku ( $new_sku )->save ();
if ($get_item) {
echo "successful<br />";
} else {
echo "item not found<br />";
}
} catch ( Exception $e ) {
echo "Cannot retrieve products from Magento: " . $e->getMessage () . "<br>";
return;
}
}
}

fclose ( $updates_handle );

This entry was posted in Magento. Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>