Skip to content

Instantly share code, notes, and snippets.

@Alexander-Pop
Last active February 1, 2019 16:58
Show Gist options
  • Save Alexander-Pop/80e6b63ebc3bd0b00b8a893838948f04 to your computer and use it in GitHub Desktop.
Save Alexander-Pop/80e6b63ebc3bd0b00b8a893838948f04 to your computer and use it in GitHub Desktop.
Programmatically Updating SKUs in Bulk on Magento #magento
<?php
/*
1) Create a CSV File with Before and After SKUs.
- In the first column, list your current SKUs and in the second column list the new SKUs.
- Do not include headings in your CSV file.
- Be sure this file is saved as a CSV file in the UTF-8 or ANSI encoding. You might run into problems with this if you create the file using Excel.
2) Upload the CSV file to the var/export directory on your Magento server so that it’s path is /var/export/sku2sku.csv.
3) Run the Script
+ If you run into the following error, don’t worry too much. Just re-run the script and see if more SKUs get updated.
- Cannot retrieve products from Magento: SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction
- If you have a lot of SKUs to update you can expect the script to take several minutes at least, to complete.
-
*/
include_once './app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$updates_file="./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);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment