Magento batch update category quick script

Just to supplement another post we made on bulk creating categories, below is a script used for bulk updating and attaching images to categories based on a specific CSV.

There is no documentation to go with this code, I’m simply posting it because a subscriber has requested it.

<?php
 
 define('MAGENTO', realpath(dirname(__FILE__)."/.."));
 require_once MAGENTO . '/app/Mage.php';
 
 umask(0);
 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 $write = Mage::getSingleton('core/resource')->getConnection('core_write');
 $count = 0;
 $headers = array();
 
 $file = fopen(MAGENTO . '/var/import/importcats.csv', 'r');
 
 $_category = Mage::getSingleton('catalog/category');
 
 while (($line = fgetcsv($file)) !== FALSE) { $count++;
 
 // First header row
 if ($count == 1) {
 foreach ($line as $id=>$col)
 $headers[$col] = $id;
 
 continue;
 }
 
 $_category->load($line[$headers['ID']]);
 
 $_category->setMetaTitle($line[$headers['meta_title']]);
 $_category->setMetaDescription($line[$headers['meta_description']]);
 $_category->setDescription($line[$headers['additional_description']]);
 $_category->setUrlKey($line[$headers['url_key']]);
 
 $sql = "INSERT INTO catalog_category_entity_varchar
 (entity_type_id, attribute_id, store_id, entity_id, value)
 VALUES
 (9, 113, 0, ".$line[$headers['ID']].", '".$line[$headers['image']]."')
 ON DUPLICATE KEY UPDATE value = '".$line[$headers['image']]."';
 ";
 
 $write->query($sql);
 
 $src = '../media/import/cat_imgs/'.$line[$headers['image']];
 $dest = '../media/catalog/category/'.$line[$headers['image']];
 
 copy($src, $dest);
 
 $_category->save();
 
 echo "Completed ".$line[$headers['name']]."<br />";
 
 }
This entry was posted in Magento and tagged , , , , . 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> <pre lang="" line="" escaped="">