I have a Magento installation, with around 60 attribute sets. I needed to add a new attribute, and then add it to all the attribute sets. Doing this manually would take ages, and there didn’t seem to be a way of doing it through the API. I therefore created a PHP script to add the new attribute directly to the database.
Essentially all I have is done is work out which tables need to be affected.
- Add the new attribute using your Magento administration control panel.
- Find the new attribute’s ID. This can be found by editting the attribute and looking at the URL.
- Alter the script below with the ID of attribute.
- For each attribute set you wish to add the new attribute to, add an array containing the attribute set name, and the attribute group name to the $add_attibutes array.
- Run the script.
<?php /* * Set your new attribute ID here */ define("NEW_ENTITY_ATTRIBUTE_ID", [NEW ATTRIBUTE ID]); define("NEW_ENTITY_TYPE_ID", 4); // this is usually 4 /* * Set your database credentials here */ define("DB_NAME", "[DATABASE NAME]"); define("DB_HOST", "[DATABASE HOST]"); define("DB_USERNAME", "[DATABASE USERNAME]"); define("DB_PASSWORD", "[DATABASE PASSWORD]"); $link = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db(DB_NAME); /* * Define the attribute sets, and groups you wish to add the new * attribute to set to here... */ $add_attibutes = array ( array ( "attribute_set_name" => "ATTRIBUTE SET 1", "attribute_group_name" => "GROUP NAME" ), array ( "attribute_set_name" => "ATTRIBUTE SET 2", "attribute_group_name" => "GROUP NAME" ), array ( "attribute_set_name" => "ATTRIBUTE SET 3", "attribute_group_name" => "GROUP NAME" ) ); foreach ( $add_attibutes as $value ) { /* * first find the attribute set id we wish to * add the attribute to from the name */ $sql = "SELECT attribute_set_id FROM mag_eav_attribute_set WHERE attribute_set_name = '" . $value['attribute_set_name'] . "' GROUP BY attribute_set_id"; $qs = mysql_query($sql); if (!$qs) { die('Could not query:' . mysql_error()); } $rs = mysql_fetch_array($qs); $attribute_set_id = $rs['attribute_set_id']; /* * now find the attribute set group ID */ $sql = "SELECT attribute_group_id FROM mag_eav_attribute_group WHERE attribute_group_name = '" . $value['attribute_group_name'] . "' AND attribute_set_id = " . $attribute_set_id; $qs = mysql_query($sql); if (!$qs) { die('Could not query:' . mysql_error()); } $rs = mysql_fetch_array($qs); $attribute_group_id = $rs['attribute_group_id']; /* * Now we can insert the attribute into the attribute set */ $sql = "INSERT INTO mag_eav_entity_attribute (entity_type_id, attribute_set_id, attribute_group_id, attribute_id) VALUES (" . NEW_ENTITY_TYPE_ID . ", " . $attribute_set_id . ", " . $attribute_group_id . ", " . NEW_ENTITY_ATTRIBUTE_ID . ");"; $result = mysql_query($sql); if (!$result) { die('Could not query:' . mysql_error()); } } ?>
This script will alter the database direct so BACKUP the database before you do anything.