-1

Can we add a new attribute in custom module? I have created one module, I want to know that, how to add a new attribute in this custom module? if it is possible, than how can do it?

9

1 Answer 1

1

You did not mention that whether you want to create a customer attribute or product attribute.

So that I am giving you the both code so that you can use as your requirement.

For Customer Attribute :

$installer = $this;
$installer->startSetup();

$installer->addAttribute("customer", "your_attribute_code",  array(
"type"     => "varchar",
"backend"  => "",
"label"    => "your_attribute_label",
"input"    => "text",
"source"   => "",
"visible"  => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique"     => false,
"note"       => ""

));

$attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "attribute_code");

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$attribute->setData("used_in_forms", $used_in_forms)
    ->setData("is_used_for_customer_segment", true)
    ->setData("is_system", 0)
    ->setData("is_user_defined", 1)
    ->setData("is_visible", 1)
    ->setData("sort_order", 100)
    ;

$attribute->save();

$installer->endSetup();

Use this code to your sql folder.

For Product Attribute :

$installer = $this;

$installer->startSetup();

$installer->addAttribute('catalog_product', 'your_attribute_code', array(
  'type'              => 'int',
  'backend'           => '',
  'frontend'          => '',
  'label'             => 'your_attribute_label',
  'input'             => 'text',
  'class'             => '',
  'source'            => '',
  'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
  'visible'           => true,
  'required'          => false,
  'user_defined'      => false,
  'default'           => '',
  'searchable'        => false,
  'filterable'        => false,
  'comparable'        => false,
  'visible_on_front'  => false,
  'unique'            => false,
  'apply_to'          => '',
  'is_configurable'   => false
));

 $installer->endSetup();

If you have any query feel free to comment.

Not the answer you're looking for? Browse other questions tagged or ask your own question.