You can add attribute from Admin Panel -> Catalog -> Attributes -> Manage Attributes.
You can also add attributes from mysql setup file of your module. MySql setup file is present inside “YourModule/sql/yourmodule_setup” directory.
In the following example, the version of my module is 0.1.0. I have added attribute for product. I have added a new Attribute Group called “Special Attributes” and then I have assigned the new attribute called “testing_attribute” into the “Special Attribute” group.
// file mysql4-install-0.1.0.php
<?php
$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
/**
* Adding Different Attributes
*/
// adding attribute group
$setup->addAttributeGroup('catalog_product', 'Default', 'Special Attributes', 1000);
// the attribute added will be displayed under the group/tab Special Attributes in product edit page
$setup->addAttribute('catalog_product', 'testing_attribute', array(
'group' => 'Special Attributes',
'input' => 'text',
'type' => 'text',
'label' => 'Testing',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'searchable' => 1,
'filterable' => 0,
'comparable' => 1,
'visible_on_front' => 1,
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$installer->endSetup();
Be careful on input and type. Input means the input type of the attribute. And type means the input type in database.
For textfield it will be:
'input' => 'text',
'type' => 'text',
For textarea it will be:
'input' => 'textarea',
'type' => 'text',
For date field it will be:
'input' => 'date',
'type' => 'datetime',
For select list it will be:
'input' => 'select',
'type' => 'text',
Here is the sample code to add datetime attribute. Please note that the type for date should be datetime
and backend should be eav/entity_attribute_backend_datetime
.
$setup->addAttribute('catalog_product', 'delivery_date', array(
'group' => 'Special Attributes',
'input' => 'date',
'type' => 'datetime',
'label' => 'Delivery',
'backend' => "eav/entity_attribute_backend_datetime",
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'searchable' => 1,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 1,
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
Adding custom options to dropdown attribute through setup file
You can also add custom options to dropdown attribute, like there is in manufacturer or color attribute.
Here is a sample code for this purpose:-
$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$setup->addAttribute('catalog_product', 'test_attribute', array(
'label' => 'Test',
'type' => 'varchar',
'input' => 'select',
'backend' => 'eav/entity_attribute_backend_array',
'frontend' => '',
'source' => 'yourmodule/source_option',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => true,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'option' => array ('value' => array('optionone' => array('Sony'),
'optiontwo' => array('Samsung'),
'optionthree' => array('Apple'),
)
),
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
));
$installer->endSetup();
In the code above, we have added a dropdown attribute named ‘Test‘. Three options (Sony, Samsung, and Apple) are added to this attribute.
Note:- In the source value, you can see yourmodule/source_option. This means, you just need to create a file YourModule/Model/Source/Option.php with the following code:-
<?php
class YourNamespace_YourModule_Model_Source_Option extends Mage_Eav_Model_Entity_Attribute_Source_Table
{
}
Following are the allowed parameters for attributes
(see Mage_Eav_Model_Entity_Setup::addAttribute)
$data = array(
'entity_type_id' => $entityTypeId,
'attribute_code' => $code,
'backend_model' => $this->_getValue($attr, 'backend', ''),
'backend_type' => $this->_getValue($attr, 'type', 'varchar'),
'backend_table' => $this->_getValue($attr, 'table', ''),
'frontend_model' => $this->_getValue($attr, 'frontend', ''),
'frontend_input' => $this->_getValue($attr, 'input', 'text'),
'frontend_input_renderer' => $this->_getValue($attr, 'input_renderer', ''),
'frontend_label' => $this->_getValue($attr, 'label', ''),
'frontend_class' => $this->_getValue($attr, 'frontend_class', ''),
'source_model' => $this->_getValue($attr, 'source', ''),
'is_global' => $this->_getValue($attr, 'global', 1),
'is_visible' => $this->_getValue($attr, 'visible', 1),
'is_required' => $this->_getValue($attr, 'required', 1),
'is_user_defined' => $this->_getValue($attr, 'user_defined', 0),
'default_value' => $this->_getValue($attr, 'default', ''),
'is_searchable' => $this->_getValue($attr, 'searchable', 0),
'is_filterable' => $this->_getValue($attr, 'filterable', 0),
'is_comparable' => $this->_getValue($attr, 'comparable', 0),
'is_visible_on_front' => $this->_getValue($attr, 'visible_on_front', 0),
'is_html_allowed_on_front' => $this->_getValue($attr, 'is_html_allowed_on_front', 0),
'is_visible_in_advanced_search'
=> $this->_getValue($attr, 'visible_in_advanced_search', 0),
'is_used_for_price_rules' => $this->_getValue($attr, 'used_for_price_rules', 1),
'is_filterable_in_search' => $this->_getValue($attr, 'filterable_in_search', 0),
'used_in_product_listing' => $this->_getValue($attr, 'used_in_product_listing', 0),
'used_for_sort_by' => $this->_getValue($attr, 'used_for_sort_by', 0),
'is_unique' => $this->_getValue($attr, 'unique', 0),
'apply_to' => $this->_getValue($attr, 'apply_to', ''),
'is_configurable' => $this->_getValue($attr, 'is_configurable', 1),
'note' => $this->_getValue($attr, 'note', ''),
'position' => $this->_getValue($attr, 'position', 0),
);
Thanks.