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

Last modified by Discovery on Fri, June 25, 2010 09:53
Source|Old Revisions  

This is an old revision of the document!


Sometimes it can be necessary to change lots of SKUs, i.e. if products are to be carried over from one model year to the next. Going into every product and changing the SKUs takes time, it can also result in typo errors. This script makes the task a little easier, particularly if using compound SKUs of the format: PRODUCTID-SIZE-COLOUR e.g. 96105-M-001, 96105-L-001, 96105-XL-001... Here the size/colour codes stay the same, it is just the main product ID that needs adjusting. This script will need modification to suit your own codes, however, in so doing, the practical experience should get you up to speed on the API.

'Installation' Notes

This script takes a csv file where each line has an old SKU followed by a comma (,) and a new SKU. The API is used to take each old/new pair and update accordingly. However, you are going to have to amend this script to suit your SKU codes. This script is specific to a shop where the SKU is made up of a product ID followed by a dash, the size code, another dash and then a colour code. The old/new pairings expected by this script are just the product ID’s, it then updates all of the products with a matching XXXX-SIZE-COLOUR pattern, matching the XXXX for all of them. Hence, the format of your .csv file is:


After you have adapted the code to your purpose I suggest making a DB backup and running a test on a couple of SKU’s. It can take a minute or two to be able to search for the new codes in the backend, Magento is slow sometimes, particularly with the API. You will also need to put in values for your API connector, hostname, user and pass. You may also need to change where the sku_update.csv file goes. Place the script out of harms way in you user directory, and not in www docroot. Run it with the command line flavour of PHP with: php sku_update.php > test.txt Check the output and if you want more verbose info on products uncomment the var_dump line. If all works as planned then run it with the full list of required changes, overnight as it can take a while.


// sku_update.php
// 17 September 2009

// Takes /var/www/html/api/sku_update.csv and updates sku codes accordingly
// File is comma separated with old pid followed by new pid

$host= 'yourshop.co.uk';
$client= new SoapClient('http://'.$host.'/index.php/api/soap/?wsdl');

// Can be added in Magento-Admin -> Web Services with role set to admin
$apiuser= 'APIUSER';
$apikey = 'APIPASS';

// Login to SOAP API with the above credentials
$sess_id= $client->login($apiuser, $apikey);

$updates_handle=fopen($updates_file, 'r');
{ while($sku_entry=fgetcsv($updates_handle, 1000, ","))
  { $old_sku=$sku_entry[0];
    echo "Updating ".$old_sku." to ".$new_sku."nn";
    { $filters = array('sku' => array('like'=>'%'.$old_sku.'%'));
      $matching_skus=$client->call($sess_id, 'product.list', array($filters));
      foreach($matching_skus as $matching_sku)
      { //var_dump($matching_sku);
        echo "SKU: ".$matching_sku['sku']." -> ".$sku_to."n";
        { $do_it=$client->call($sess_id, 'product.update', array($matching_sku['sku'], array('sku'=>$sku_to)));
        catch (Exception $e)
        { echo "WSDL call failed!n";
    catch (Exception $e)
    { echo "Cannot retrieve products from Magenton".$e."nn";