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?
-
For product check this mageplaza.com/magento-2-module-development/…– Kingshuk DebCommented Apr 13, 2017 at 6:12
-
For customer check this mageplaza.com/magento-2-module-development/…– Kingshuk DebCommented Apr 13, 2017 at 6:12
-
For category check this inchoo.net/magento-2/add-category-attribute-magento-2– Kingshuk DebCommented Apr 13, 2017 at 6:13
-
But, how can display in custom module– Rushikesh SolankiCommented Apr 13, 2017 at 6:15
-
For which entity you want to add an attribute?– Kingshuk DebCommented Apr 13, 2017 at 6:16
|
Show 4 more comments
1 Answer
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.