Is there any clean way to add custom field to all product attributes in Magento?
Catalog -> Attributes -> Manage Attributes -> (Add new/Edit) Attribute
page.
Something like it can be done for catalog category:
$setup = new Mage_Eav_Model_Entity_Setup("core_setup");
// below code will add text attribute
$setup->addAttribute("catalog_category", "attribute_code", array(
"group" => "General",
"input" => "text",
"type" => "varchar",
"label" => "Attribute label",
"backend" => "",
"visible" => 1,
"required" => 0,
"user_defined" => 1,
"global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
There is no record in eav_entity_type table for attributes, so i can not use this method.
Is there any suggestion on the best way for adding this custom field for attributes.
Best Answer
The only attribute connection in the
eav_entity_type
table is a field (addition_attribute_table
) which points to additional attribute metadata for each type.The fields in the
Catalog > Attributes > Manage Attributes
create/edit views are attribute metadata and map to the columns in theeav_attribute
andcatalog_eav_attribute
tables. The canonical attribute record and cross-entity metadata are in the former table, while catalog attribute metadata are in the latter table.In order to have another field in the referenced view, you either need to rewrite the
Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Main
class or (more flexibly) add another tab which contains the input for your custom field. You will also need to add your column to thecatalog_eav_attribute
table.You might want to rewrite the
Mage_Catalog_Model_Resource_Product_Attribute_Collection
model if you need to conveniently apply filtering conditions based on your field, and you might consider rewritingMage_Catalog_Model_Resource_Setup::_prepareValues()
if you want to specify a default value for your field.Pretty neat!