How to add more option to an attribute programmatically in magento

 

If you have a attribute available (In Magento Admin) and the data type of that attribut is drop down type. Suppose you get the huge values that needs to be entered as a option value for your magento attribute. Then you have two ways to add those values

i) Add them manually
ii) Add those option values through normal PHP script

If the option values are not more then obviousely the 1st option is quite fine. But if option valus are huge then definately you to import through PHP script only. But for that you must have to understand the relation between the tables that will contain the data regarding your attribut option.
There are basically three tables associated that holds the attribute option value in magento

eav_attribute
eav_attribute_option
eav_attribute_option_value

Below image will show the relaion of these tables
eav attribute in magento

PHP code to import attribute option into it.

<?php
//die("breaking");
require_once('../mydb.php');

$query_make = "select * from option_value";
$query_run = mysql_query($query_make);
while($make_row =mysql_fetch_array($query_run))
{
//print_r($make_row);
$insert_attribute_option ="INSERT INTO `eav_attribute_option` (
`option_id` ,
`attribute_id` ,
`sort_order`
)
VALUES (
NULL , '--Attributeid--', '0'
)";
mysql_query($insert_attribute_option) or die("false mysql query1");
$insert_id = mysql_insert_id();

$insert_attribute_option_value1 ="insert into `eav_attribute_option_value` (`value_id`,`option_id`,`store_id`,`value`) values(NULL,".$insert_id.",1,'".$make_row['value_name']."')";
mysql_query($insert_attribute_option_value1) or die("unable to run query1");
/*I am running two times this query because their are two stores are available with my website . So store id is 1 and 0*/
$insert_attribute_option_value2 ="insert into `eav_attribute_option_value` (`value_id`,`option_id`,`store_id`,`value`) values(NULL,".$insert_id.",0,'".$make_row['value_name']."')";
mysql_query($insert_attribute_option_value2) or die("unable to run query2");

}

?>

Note: –Attributeid– this is the id of the attribute in which you are going to insert the data.

Related posts:

The following two tabs change content below.

Chandra Shekhar

Consultant at Infogain
Chandra Shekhar is a LAMP Developer,Thinker,Blogger and currently working as Magento(Ecommerce) specialist.. I am always open to learn new things in LAMP as well as other newer technology. In the journey of my last 9 years of web and product development, I have decided to share my knowledge and experience to Open Source Community in 2011. In this regard I have started CuteHits.com. Since then I am actively sharing my knowledge on various LAMP technology through CuteHits.com’s technical ... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)