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.

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...