7

I want to programmatically add new product option values in a data upgade script of my module. How can I do this?

11

Add bellow code in your upgrade script file

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

$attributeCode = 'manufacturer';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);

if ($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $option['attribute_id'] = $attribute->getId();
    $option['value']        =  array('Red','Black', 'Yellow');
    $installer->addAttributeOption($option);
}

//OR
/*
if($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $option['attribute_id'] = $attribute->getId();
    $option['value']['r'][0] = 'Red';
    $option['value']['b'][1] = 'Black';
    $option['value']['y'][2] = 'Yellow';
    $installer->addAttributeOption($option);
}*/

$installer->endSetup();

Check duplicate option value code:

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

$attributeCode = 'manufacturer';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);

 if($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $newOptions =  array('Red','Black', 'Yellow');
    $exitOptions =  array();
    $options = Mage::getModel('eav/entity_attribute_source_table')
                        ->setAttribute($attribute)
                        ->getAllOptions(false);
    foreach ($options as $option) {
        if (in_array($option['label'], $newOptions)) {
            array_push($exitOptions, $option['label']);
        }else {

        }
    }
    $insertOptions = array_diff($newOptions, $exitOptions);
    if(!empty($insertOptions)) {
        $option['attribute_id'] = $attribute->getId();
        $option['value']        =  $insertOptions;  
        $installer->addAttributeOption($option);
    }            
}

$installer->endSetup();
  • 1
    What is the meaning of indexes 'r', 'b', 'y' in $option['value']['r'][0] = 'Red';? – Anton Belonovich Feb 7 '16 at 6:36
  • That creates a broken drop-down option here, on Magento CE 1.9. Table eav_attribute_option gets one new row, but without a corresponding row in eav_attribute_option_value. Must be something with the $option array structure. – Anse Mar 8 '17 at 10:43
  • Please can you help me to check the attribute value if that value is already available. So duplicate value not insert in attribute – Purushotam Sharma Jul 13 '17 at 6:00
  • @Purushotam Sharma : Updates ans pls check and let me know it is working or not bec i am not tested code. – Abdul Jul 13 '17 at 8:05
  • hello @Abdul! its not adding option while running this script – SagarPPanchal Mar 6 '18 at 12:39
10

try this,

for single value:-

$arg_attribute = 'color';
$arg_value = 'red';

$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;
$option['value']['any_option_name'][0] = $arg_value;

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

for multiple values:-

$arg_attribute = 'color';
$key_data = array('red','black','orange');
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
foreach($key_data as $key_value)
{   
    $option = array();
    $arg_value = trim($key_value);
    $attr_id = $attr->getAttributeId();
    $option['attribute_id'] = $attr_id;
    $option['value']['any_option_name'][0] = $arg_value;
    $setup->addAttributeOption($option);
}

'any_option_name' would be a color_name (ex: red) arg_value would be it's integer optionId afaik.

The thing that would also need to be acquired first, is what the next unused optionId is. To be used for this new attribute option.

6

For example, you want to add Men value to gender option.

First you have to create your upgrade script in module directory, e.g. app/code/local/MyCompany/MyModule/data/mymodule_setup/data-upgrade-0.1.0-0.1.1.php.

Then fill it with code like this:

<?php

$this->startSetup();

$genderAttribute = Mage::getModel('eav/entity_attribute')
    ->loadByCode('catalog_product', 'gender'); // 'gender' is your attribute code

$this->addAttributeOption([
    'attribute_id' => $genderAttribute->getId(),
    'value' => [[0 => 'Men', 1 => 'Men', 10 => 'Men']] // array indexes are store IDs
]);

$this->endSetup();
1

The following code add attributes options programmatically magento 1.

Please refer for detailed explanation how to read from CSV and compare with existing attribute options https://www.pearlbells.co.uk/add-attribute-options-magento-scripts/

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

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
    echo 'Insert new option : '.$options[ $i ].PHP_EOL;

}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.