Magento 1.9 PHP Script – Update Category Attributes Programmatically

categorymagento-1.9PHPscript

I can't seem to figure out how to best update hundreds of category attributes with a script. Any suggestions as the best way to approach this? I won't be using the installer or anything like that.

Best Answer

If you have many categories and want to update few attributes than you can use code similar to this below. It's fast and saves only values for these attributes which you want to update. You can run this script from browser or from terminal (terminal is better for cases like you described).

<?php

require 'app/Mage.php';
Mage::app();

$resource = Mage::getResourceModel('catalog/category');

// here get category collection

foreach($collection as $category) {
    $category->setStoreId(0);    // 0 for default scope (All Store Views)
    $category->setData('attribute_code', $value);
    $resource->saveAttribute($category, 'attribute_code');
}
Related Topic