Home » Php » php – Add new values to a attribute option in magento

php – Add new values to a attribute option in magento

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&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');

Answer:

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.

Answer:

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')

Answer:

Important! (Hopefully this helps somebody, cause I was stuck like 2h with this issue)

If you are using special characters (such as ä, ö, ü, ß, ×, …) make sure to encode them properly!

array_walk($manufacturers , create_function('&$val', '$val = utf8_encode($val);'));

Answer:

Here a simple and very fast way….

Delete an option

/* My option value */
$value = 'A value';
/* Delete an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
    $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID from value...
    if ($option_id) {
        $options['delete'][$option_id] = true; 
        $attribute->setOption($options)->save();
    }
}
/* END ! */

Add or update an option

/* Add/Update an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
    $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID...
    $options['order'][$option_id] = 10; // Can be removed... Set option order...
    $options['value'][$option_id] = array(
        0 => $value, // Admin Store - Required !
        1 => $value, // Store id 1 - If U want ! Can be removed
    );
    $attribute->setDefault(array($option_id)); /* If you want set option as default value */
    $attribute->setOption($options)->save(); /* That's all */
}
/* END ! */  

Answer:

In my tutorial I am explaining how to read the options from the CSV and create the options pro grammatically.

http://www.pearlbells.co.uk/add-attribute-options-magento-scripts/
please click the tutorial for further explanation

function createAttribute($options) {
$option = array('attribute_id' => 
Mage::getModel('eav/entity_attribute')->getIdByCode(
     Mage_Catalog_Model_Product::ENTITY, 
     'color'
    )
);

for ($i = 0; $i < count($options); $i++) {
    $option['value']['option'.$i][0] = $options[ $i ]; // Store View
    $option['value']['option'.$i][1] = $options[ $i ]; // Default store view
    $option['order']['option'.$i] = $i; // Sort Order

}

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

Answer:

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

Agree with Arvind but it’s only works for insert the single option value and if you want to perform insert multiple option value then you just needs to replace the code from “$option[‘value’][$key.’‘.$manufacturer] = $manufacturer;” to “$option[‘values’][$key.’‘.$manufacturer] = $manufacturer;” to this.

below is the final code

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

            $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['values'][$key . '_' . $manufacturer] = $manufacturer;
            }

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

I hope its works for insertion multiple option.