Custom attributes can be added through
Magento Admin Panel > Catalog > Attributes > Manage Attributes
, but you can also add attributes through the SQL setup file of your module.
Example
$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); $entityTypeId = $setup->getEntityTypeId('catalog_product'); $attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId); $setup->addAttribute($entityTypeId, "my_stock_alert", array( 'group' => 'Inventory', 'input' => 'text', 'type' => 'text', 'label' => 'Stock alert percent', 'required' => false, 'user_defined' => true, 'visible' => true, 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'visible_in_advanced_search' => false, 'used_in_product_listing' => false, 'apply_to' => array('bundle,virtual,simple'), 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 'unique' => false, 'default' => '0', 'note' => 'This note will appear below the input field as a hint', //Add hint 'frontend_class' => 'validate-percents' //Add validation to percentage ));
List of allowed parameters for attributes
Extracted from Mage_Eav_Model_Entity_Setup::addAttribute:
- entity_type_id
- attribute_code
- backend_model
- backend_type
- backend_table
- frontend_model
- frontend_input
- frontend_input_renderer
- frontend_label
- frontend_class
- source_model
- is_global
- is_visible
- is_required
- is_user_defined
- default_value
- is_searchable
- is_filterable
- is_comparable
- is_visible_on_front
- is_html_allowed_on_front
- is_visible_in_advanced_search
- is_used_for_price_rules
- is_filterable_in_search
- used_in_product_listing
- used_for_sort_by
- is_unique
- apply_to
- is_configurable
- note
- position
List of validation options
Validation options available in the Magento admin backend are limited to a few:
Validation options are coming from the js/prototype/validation.js.This file contains all the error messages and regular expressions.
- validate-no-html-tags
- validate-select
- required-entry
- validate-number
- validate-number-range
- validate-digits
- validate-alpha
- validate-code
- validate-alphanum
- validate-alphanum-with-spaces
- validate-street
- validate-phoneStrict
- validate-phoneLax
- validate-fax
- validate-date
- validate-email
- validate-password
- validate-cpassword
- validate-url
- validate-clean-url
- validate-identifier
- validate-xml-identifier
- validate-ssn
- validate-zip
- validate-zip-international
- validate-date-au
- validate-currency-dollar
- validate-one-required
- validate-one-required-by-name
- validate-not-negative-number
- validate-state
- validate-new-password
- validate-cc-type
- validate-cc-type
- validate-cc-type-select
- validate-cc-exp
- validate-cc-cvn
- validate-data
- validate-css-length
- validate-length
- validate-percents
- required-file
- validate-cc-ukss