Add new values to a attribute option in magento

Tony picture Tony · Feb 20, 2011 · Viewed 17.1k times · Source

I am trying to add new values to attribute option in magento using a script to speed up the process since I have over 2,000 manufactuers

Answer

Jonathan Day picture Jonathan Day · Feb 20, 2011

Here is a piece of code that I used to perform exactly this task. Create a custom module (using ModuleCreator as a tool) and then create a mysql4-install-0.1.0.php under the sql/modulename_setup folder. It should contain the following (adapted to your own data, of course!).

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$aManufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$aOption = array();
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, 'manufacturer');

for($iCount=0;$iCount<sizeof($aManufacturers);$iCount++){
   $aOption['value']['option'.$iCount][0] = $aManufacturers[$iCount];
}
$installer->addAttributeOption($aOption);

$installer->endSetup();    

More documentation on the Magento wiki if you want.

If you don't want to do it in a custom module, you could just create a php file that starts with:

require_once 'app/Mage.php';
umask(0);
Mage::app('default');