I want to programmatically add new product option values in a data upgade script of my module. How can I do this?
5 Answers
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();
-
1What is the meaning of indexes
'r','b','y'in$option['value']['r'][0] = 'Red';?Anton Belonovich– Anton Belonovich2016-02-07 06:36:10 +00:00Commented Feb 7, 2016 at 6:36 -
1That creates a broken drop-down option here, on Magento CE 1.9. Table
eav_attribute_optiongets one new row, but without a corresponding row ineav_attribute_option_value. Must be something with the$optionarray structure.Anse– Anse2017-03-08 10:43:49 +00:00Commented Mar 8, 2017 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 attributePurushotam Sharma– Purushotam Sharma2017-07-13 06:00:02 +00:00Commented Jul 13, 2017 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– Abdul2017-07-13 08:05:34 +00:00Commented Jul 13, 2017 at 8:05
-
hello @Abdul! its not adding option while running this scriptSagarPPanchal– SagarPPanchal2018-03-06 12:39:16 +00:00Commented Mar 6, 2018 at 12:39
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.
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();
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);
}
I tested it on Magento 1 (OpenMage LTS 19.4.11)
This code works correctly:
$attributeCode = 'manufacturer';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$option = [];
$option['attribute_id'] = $attribute->getId();
$option['value'] = [
'option_1' => ['value 1'],
'option_2' => ['value 2'],
'option_3' => ['value 3'],
];
$installer->addAttributeOption($option);
You can use it not only on the install script