Batch change descriptions programmatically in Magento

This is a simple script to bulk update Magento products’ descriptions quickly and easily. It takes as its input a two column CSV, which simply has the product SKU in the first column, and the new description in the second column – this CSV is uploaded to the web root of your site, along with the script file.

Script To Bulk Update Magento Products’ Descriptions

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Change the name of the CSV file in the next line to match your own CSV :

$file_handle = fopen("descriptions.csv", "r");

The script then loops through the file line by line, gets the SKU from the first column and the new description from the second. It then tries to load the product – if it’s successful in doing so then it sets the new description as the product’s description, then saves the products and reports success.

while (!feof($file_handle) ) {
	$line_of_text = fgetcsv($file_handle, 1024);
	$productSku = $line_of_text[0];
	$newDescription = $line_of_text[1];
	$simpleProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSku);
	if($simpleProduct) {
		$simpleProduct->setDescription($newDescription);
		$simpleProduct->save();
		echo "Updated product " . $productSku . "<br>";
	}
}
fclose($file_handle);
?>

Run the file through your browser or – better still – over SSH. The latter is advised if you’ve got a large number of descriptions to amend, as it’s likely that your browser would time out.

14 comments

  • Colin

    That’s a great script, but what if you have more than one store running off your Magento install? It works for the main product, but not the default one for a specific shop.

    • Giles Bennett (author)

      Colin,

      That’s right – if that were the case, then you would either look to run the change of descriptions via an import of a CSV, or (if you wanted to stick with using a script) you would need to adjust the script to set the descriptions on a per-store basis.

      Kind regards,

      Giles

  • Scott

    This is an awesome little bit of code. Works wonders and is much better than spending $20 – $100 or more for an extension do something like this. Thanks Giles!

    • Giles (author)

      Scott,

      Glad you find it useful.

      Kind regards,

      Giles

  • Nick

    Giles, thanks for this, are there any attributes that this would not be suitable for?

    • Giles (author)

      Nick,

      Not that I can think of.

      Kind regards,

      Giles

  • Nick

    HI Giles, I don’t see how this distinguishes between updating the long or short description – or does it do both?

    • Giles (author)

      Nick,

      setDescription() updates the long description only – you’d need to add another column to the CSV and another line using setShortDescription() to set the short description.

      Kind regards,

      Giles

      • Nick

        Thanks GIles …

        • Giles (author)

          Nick,

          You’re welcome.

          Kind regards,

          Giles

  • Larry

    You did a great job. It do help a lot. Can you provide a similar program to update inventory? Thanks.

    • Giles (author)

      Larry,

      Yes, we have a couple of those in our library, so feel free to drop us a line at giles@hummingbirduk.com.

      Kind regards,

      Giles

  • ijasul rahim

    It worked..thank a lot.Can you provide a similar program to update meta keywords?
    Thanks once again

    • Giles (author)

      Ijasul,

      You would use the same script, but instead of :

      $simpleProduct->setDescription($newDescription);
      

      you would use

      $simpleProduct->setMetaKeyword($newDescription);
      

      (Note that it’s Keyword, not Keywords).

      Kind regards,

      Giles

Leave a Reply

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

Want to talk to us about your project?