Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new values to a attribute option in magento

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

like image 466
Tony Avatar asked Feb 20 '11 00:02

Tony


3 Answers

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');
like image 184
Jonathan Day Avatar answered Nov 04 '22 10:11

Jonathan Day


Answer of Jonathan is correct. But if you want to perform it without installer i.e in any other code, then you might find this helpful:

$arg_attribute = 'manufacturer';
$manufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');

$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();

$option['attribute_id'] = $attr_id;
foreach ($manufacturers as $key=>$manufacturer) {
    $option['value'][$key.'_'.$manufacturer][0] = $manufacturer;
}

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);

More information can be found here.

like image 34
Arvind Bhardwaj Avatar answered Nov 04 '22 11:11

Arvind Bhardwaj


I have created a function to dynamically add option to attribute

public function addAttributeOptions($attributeCode, $argValue)
{
    $attribute = Mage::getModel('eav/config')
        ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);


    if ($attribute->usesSource()) {

        $id = $attribute->getSource()->getOptionId($argValue);
        if ($id) 
            return $id;
    }

    $value = array('value' => array(
            'option' => array(
                    ucfirst($argValue),
                    ucfirst($argValue)
                )
        )
    );

    $attribute->setData('option', $value);
    $attribute->save();

    //return newly created option id
    $attribute = Mage::getModel('eav/config')
        ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
    if ($attribute->usesSource()) {
        return $attribute->getSource()->getOptionId($argValue);
    }
}

You can add an option to your attribute by providing code and option value

$this->addAttributeOptions('unfiorm_type', 'leotartd')
like image 31
Tofeeq Avatar answered Nov 04 '22 11:11

Tofeeq