How can I create an attribute as "not configurable" from an install script?
If you go through the Mage_Eav_Model_Entity_Setup::addAttribute()
method, you can find that the options are getting filtered in the Mage_Eav_Model_Entity_Setup::_prepareValues()
method.
The _prepareValues
method only allows the following options to go through:
'backend_model', 'backend_type', 'backend_table', 'frontend_model',
'frontend_input', 'frontend_label', 'frontend_class',
'source_model', 'is_required', 'is_user_defined', 'default_value',
'is_unique', 'note', 'is_global'
Any option that needs to be set, and is not in previous list, cannot be passed into the addAttribute
method.
The only exception to those are: sort_order
, group
, user_defined
and option
. Those are used independently from the _prepareValues
method.
One way to work around this issue, is to update the attribute after it is created, using the Mage_Eav_Model_Entity_Setup::updateAttribute()
method.
$this->updateAttribute(
Mage_Catalog_Model_Product::ENTITY,
'size',
'is_configurable',
false
)
You should actually use Mage_Catalog_Model_Resource_Setup::addAttribute()
instead of Mage_Eav_Model_Entity_Setup::addAttribute()
As you have found out in the code, Mage_Eav_Model_Entity_Setup::addAttribute()
only allows the following options:
backend
type
table
frontend
input
label
frontend_class
source
required
user_defined
default
unique
note
global
Whereas Mage_Catalog_Model_Resource_Setup::addAttribute()
allows all of the above, plus the following options:
input_renderer
global
visible
searchable
filterable
comparable
visible_on_front
wysiwyg_enabled
is_html_allowed_on_front
visible_in_advanced_search
filterable_in_search
used_in_product_listing
used_for_sort_by
apply_to
position
is_configurable
used_for_promo_rules
You can use this class even if you have defined another setup class in your config.xml
:
$installer = $this;
$installer->startSetup();
// Create your tables, etc
$catalogInstaller = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$catalogInstaller->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'size', array(
'type' => 'int',
'label' => 'Color',
'input' => 'select',
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible_in_advanced_search' => true,
'is_configurable' => false,
));
$installer->endSetup();