Magento manage attributes from model

Magento manage attributes from model

You can create and remove Magento product attributes from model. It’s quite simple if you know how.

Ok. Lets create attribute:

$entityTypeId = 'catalog_product';
$code = 'my_attribute';
 
$attr = array(
  'entity_type_id' => $entity_type_id,
  'backend_type' => 'int',
  'is_user_defined' => 1,
  'frontend_input' => 'text',
  'is_visible' => 0,
);
	 
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$r = $setup->addAttribute($entityTypeId, $code, $attr);

And now we can remove attribute from model:

$entityTypeId = 'catalog_product';
$code = 'my_attribute';

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
	
$info = $setup->getEntityType($entityTypeId);
	
$setup->removeAttribute($info['entity_type_id'], $code);