Add new values to a attribute option in Magento
Asked Answered
E

7

7

I am trying to add new values to an attribute option in Magento using a script to speed up the process since I have over 2,000 manufacturers.

E answered 20/2, 2011 at 0:32 Comment(0)
M
17

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');
Madrid answered 20/2, 2011 at 0:54 Comment(3)
how do I preform this without the installer? Its for a production website.E
error message? I can assure you that it does work, I've used exactly that code. Using an installer is the preferred method for production sites.Madrid
@E btw, the variable is called installer but that does not mean that it's reinstalling the whole Magento stack. There is a convenience class that is used for making installs, upgrades and changes. This is the correct Magento approach for your task.Madrid
C
8

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.

Cotquean answered 3/5, 2012 at 4:4 Comment(0)
T
5

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')
Tyrannous answered 18/4, 2016 at 8:53 Comment(1)
Thanks for sharing!Sterculiaceous
G
1

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);'));
Grim answered 7/12, 2012 at 11:27 Comment(1)
Since we all have PHP/5.3, a more readable and faster version would be $manufacturers = array_map('utf8_encode',$manufacturers);Lassalle
L
1

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 ! */  
Lixivium answered 28/11, 2015 at 13:16 Comment(0)
O
0

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.

Orly answered 6/9, 2018 at 15:1 Comment(0)
T
0

If you want to add new values for differ store, you cat try this

    $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    $table = $installer->getTable('eav/attribute_option_value');
    $store = Mage::getModel('core/store')->load("store_code", "code");
    $sizes = ["default_value" => "store_value", "default_value_1" => "store_value_1"];
    $attribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product', 'size');
    $storeViewAttributeOptions = 
    Mage::getResourceModel('eav/entity_attribute_option_collection')
      ->setAttributeFilter($attribute->getId())
      ->setStoreFilter($store->getId())
      ->setPositionOrder()
      ->load();
    foreach ($storeViewAttributeOptions as $storeViewAttributeOption) {
        if (in_array($storeViewAttributeOption->getDefaultValue(), array_keys($sizes)) ){
        $data = [
            'option_id' => $storeViewAttributeOption->getId(),
            'store_id'  => $store->getId(),
            'value'     => $sizes[$storeViewAttributeOption->getDefaultValue()],
        ];
        $installer->getConnection()->insert($table, $data);
        }
    }
    $installer->endSetup();
Trichinopoly answered 10/5, 2023 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.