Reputation: 1356
How can we create an attribute for product programatically in magento CE 1.9? I have been trying out multiple ways for creating an attribute for product that has a boolean value by default set to false and assigned to Default attribute set, but not visible/editable by any admin panel user.
my Model/Resource/Eav/Mysql4/Setup.php
<?php
class Mofosys_Quickbuy_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup {
protected function _prepareValues($attr) {
$data = parent::_prepareValues($attr);
$data = array_merge($data, array(
'apply_to' => $this->_getValue($attr, 'apply_to'),
'frontend_input_renderer' => $this->_getValue($attr, 'input_renderer'),
'is_comparable' => $this->_getValue($attr, 'comparable', 0),
'is_configurable' => $this->_getValue($attr, 'is_configurable', 0),
'is_filterable' => $this->_getValue($attr, 'filterable', 0),
'is_filterable_in_search' => $this->_getValue($attr, 'filterable_in_search', 0),
'is_global' => $this->_getValue(
$attr,
'global',
Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
),
'is_html_allowed_on_front' => $this->_getValue($attr, 'is_html_allowed_on_front', 0),
'is_searchable' => $this->_getValue($attr, 'searchable', 0),
'is_used_for_promo_rules' => $this->_getValue($attr, 'used_for_promo_rules', 0),
'is_visible' => $this->_getValue($attr, 'visible', 0),
'is_visible_on_front' => $this->_getValue($attr, 'visible_on_front', 0),
'is_wysiwyg_enabled' => $this->_getValue($attr, 'wysiwyg_enabled', 0),
'is_visible_in_advanced_search' => $this->_getValue($attr, 'visible_in_advanced_search', 0),
'position' => $this->_getValue($attr, 'position', 0),
'used_for_sort_by' => $this->_getValue($attr, 'used_for_sort_by', 0),
'used_in_product_listing' => $this->_getValue($attr, 'used_in_product_listing', 0),
));
return $data;
}
}
?>
my etc/config.xml
<global>
<resources>
<mofosys_quickbuy_setup>
<setup>
<module>Mofosys_Quickbuy</module>
<class>Mofosys_Quickbuy_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
</mofosys_quickbuy_setup>
</resources>
</global>
my sql/mofosys_quickbuy_setup/install-0.0.1.php
<?php
// Installer file to create an attribute name "approved" inside Default attribute set
$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
// the attribute added will be displayed under the group/tab Special Attributes in product edit page
$setup->addAttribute('catalog_product', 'approved', array(
'group' => 'Default',
'input' => 'select',
'type' => 'int',
'default' => '0',
'label' => 'Testing',
'backend' => '',
'visible' => 0,
'required' => 0,
'user_defined' => 0,
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$installer->endSetup();
?>
Upvotes: 0
Views: 2096
Reputation: 41
Try this, it should work, it just worked fine for me, just put this code in your install.php
$installer = $this;
$installer->startSetup();
// Set data:
$attributeName = 'Approve'; // Name of the attribute
$attributeCode = 'approve'; // Code of the attribute
$attributeGroup = 'General'; // Group to add the attribute to
$attributeSetIds = array(4); // Array with attribute set ID's to add this attribute to. (ID:4 is the Default Attribute Set)
// Configuration:
$data = array(
'type' => 'select', // Attribute type
'input' => 'boolean', // Input type
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, // Attribute scope
'required' => false, // Is this attribute required?
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'used_in_product_listing' => true,
'default_value_yesno' => '0',
// Filled from above:
'label' => $attributeName
);
// Create attribute:
// We create a new installer class here so we can also use this snippet in a non-EAV setup script.
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->addAttribute('catalog_product', $attributeCode, $data);
// Add the attribute to the proper sets/groups:
foreach($attributeSetIds as $attributeSetId)
{
$installer->addAttributeToGroup('catalog_product', $attributeSetId, $attributeGroup, $attributeCode);
}
$installer->endSetup();
Upvotes: 1