Magento 1.9 – How to Programmatically Add Custom Product Attribute to Attribute Set

attribute-setmagento-1.9PHP

Is there a way to add a custom Attribute (DB-Column, whatever) to product attribute sets?

I tried the following but it I get the error 'Wrong Entity ID':

/** @var Mage_Catalog_Model_Resource_Setup $this */
$this->addAttribute('eav/entity_attribute_set', 'pim_id', array(
    'group'         => 'General Information',
    'input'         => 'text',
    'type'          => 'text',
    'label'         => 'PIM ID',
    'backend'       => '',
    'visible'       => false,
    'required'      => false,
    'is_unique'     => true,
    'visible_on_front' => false,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

Best Answer

Try this

<?php  
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute('catalog_product', 'pim_id', array(
            'group'           => 'General',
            'label'           => 'PIM ID',
            'input'           => 'text',
            'type'            => 'varchar',
            'required'        => 0,
            'visible_on_front'=> 1,
            'filterable'      => 0,
            'searchable'      => 0,
            'comparable'      => 0,
            'user_defined'    => 1,
            'is_configurable' => 0,
            'global'          => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
            'note'            => '',
));
$installer->endSetup();
?>

Dropdown input with options

<?php  
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute('catalog_product', 'addtocart_settings', array(          
            'group'                 => 'General',
            'label'                 => 'Add To Cart Settings',
            'input'                 => 'select',
            'type'                  => 'varchar',
            'required'              => 0,
            'visible_on_front'      => false,
            'filterable'            => 0,
            'filterable_in_search' => 0,
            'searchable'            => 0,
            'used_in_product_listing' => true,
            'visible_in_advanced_search' => false,
            'comparable'      => 0,
            'user_defined'    => 1,
            'is_configurable' => 0,
            'global'          => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
            'option'          => array('values' => array('Default', 'Available at authorized dealer','Not available yet')),
            'note'            => ''
));

$installer->endSetup(); 

?>
Related Topic