Tony
Tony

Reputation: 81

Add new values to a attribute option in Magento

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.

Upvotes: 7

Views: 18474

Answers (7)

Vitaliy Talimonchuk
Vitaliy Talimonchuk

Reputation: 1

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();

Upvotes: 0

Vikas Gupta
Vikas Gupta

Reputation: 11

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.

Upvotes: 0

Tofeeq
Tofeeq

Reputation: 2683

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

Upvotes: 5

Imaginaerum
Imaginaerum

Reputation: 779

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 ! */  

Upvotes: 1

Steven
Steven

Reputation: 11

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

Upvotes: 1

Arvind Bhardwaj
Arvind Bhardwaj

Reputation: 5291

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.

Upvotes: 8

Jonathan Day
Jonathan Day

Reputation: 18692

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

Upvotes: 17

Related Questions