{"id":7260,"date":"2019-09-23T10:28:21","date_gmt":"2019-09-23T04:58:21","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7260"},"modified":"2019-09-27T13:36:43","modified_gmt":"2019-09-27T08:06:43","slug":"magento-2-add-product-attribute-programmatically","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-product-attribute-programmatically\/","title":{"rendered":"Magento 2 &#8211; How to Add Product Attribute Programmatically"},"content":{"rendered":"<p>In this post I am going to explain how to add product attribute programmatically in magento 2.<\/p>\n<p>We are going to create three custom product attributes. Yes\/No, Select Option and Text field.<\/p>\n<p>We need to create the data setup script(InstallData.php) with parameters of the new attribute.<\/p>\n<p>File path: app\/code\/Ht\/Mymodule\/Setup\/InstallData.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Setup;\r\n\r\nuse Magento\\Eav\\Setup\\EavSetup;\r\nuse Magento\\Eav\\Setup\\EavSetupFactory;\r\nuse Magento\\Framework\\Setup\\InstallDataInterface;\r\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\r\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\r\n\r\nclass InstallData implements InstallDataInterface \r\n{\r\n    private $eavSetupFactory;\r\n\r\n    public function __construct(EavSetupFactory $eavSetupFactory) \r\n    {\r\n        $this-&gt;eavSetupFactory = $eavSetupFactory;\r\n    }\r\n\r\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) \r\n    {\r\n        $eavSetup = $this-&gt;eavSetupFactory-&gt;create(['setup' =&gt; $setup]);\r\n\r\n        $eavSetup-&gt;removeAttribute(\\Magento\\Catalog\\Model\\Product::ENTITY, 'custom_yes_no');\r\n        $eavSetup-&gt;addAttribute(\\Magento\\Catalog\\Model\\Product::ENTITY, 'custom_yes_no', [\r\n            'group' =&gt; 'Product Details',\r\n            'type' =&gt; 'int',\r\n            'sort_order' =&gt; 200,\r\n            'backend' =&gt; '',\r\n            'frontend' =&gt; '',\r\n            'label' =&gt; 'Custom Yes\/No',\r\n            'input' =&gt; 'boolean',\r\n            'class' =&gt; '',\r\n            'source' =&gt; 'Magento\\Eav\\Model\\Entity\\Attribute\\Source\\Boolean',\r\n            'global' =&gt; \\Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute::SCOPE_GLOBAL,\r\n            'visible' =&gt; true,\r\n            'required' =&gt; false,\r\n            'user_defined' =&gt; false,\r\n            'default' =&gt; '',\r\n            'searchable' =&gt; false,\r\n            'filterable' =&gt; false,\r\n            'comparable' =&gt; false,\r\n            'visible_on_front' =&gt; false,\r\n            'used_in_product_listing' =&gt; true,\r\n            'unique' =&gt; false,\r\n            'apply_to' =&gt; ''\r\n        ]);\r\n\r\n        $eavSetup-&gt;removeAttribute(\\Magento\\Catalog\\Model\\Product::ENTITY, 'custom_dropdown');\r\n        $eavSetup-&gt;addAttribute(\\Magento\\Catalog\\Model\\Product::ENTITY, 'custom_dropdown', [\r\n            'group' =&gt; 'Product Details',\r\n            'type' =&gt; 'int',\r\n            'backend' =&gt; '',\r\n            'frontend' =&gt; '',\r\n            'sort_order' =&gt; 210,\r\n            'label' =&gt; 'Custom Dropdown',\r\n            'input' =&gt; 'select',\r\n            'class' =&gt; '',\r\n            'source' =&gt; 'Ht\\Mymodule\\Model\\Source\\Customdropdown',\r\n            'global' =&gt; \\Magento\\Eav\\Model\\Entity\\Attribute\\ScopedAttributeInterface::SCOPE_GLOBAL,\r\n            'visible' =&gt; true,\r\n            'required' =&gt; false,\r\n            'user_defined' =&gt; false,\r\n            'default' =&gt; '',\r\n            'searchable' =&gt; false,\r\n            'filterable' =&gt; false,\r\n            'comparable' =&gt; false,\r\n            'visible_on_front' =&gt; false,\r\n            'used_in_product_listing' =&gt; true,\r\n            'apply_to' =&gt; ''\r\n        ]);\r\n\r\n        $eavSetup-&gt;removeAttribute(\\Magento\\Catalog\\Model\\Product::ENTITY, 'custom_text_field');\r\n        $eavSetup-&gt;addAttribute(\\Magento\\Catalog\\Model\\Product::ENTITY, 'custom_text_field', [\r\n            'group' =&gt; 'Product Details',\r\n            'type' =&gt; 'varchar',\r\n            'backend' =&gt; '',\r\n            'frontend' =&gt; '',\r\n            'sort_order' =&gt; 220,\r\n            'label' =&gt; 'Custom Text Field',\r\n            'input' =&gt; 'text',\r\n            'class' =&gt; '',\r\n            'global' =&gt; \\Magento\\Eav\\Model\\Entity\\Attribute\\ScopedAttributeInterface::SCOPE_GLOBAL,\r\n            'visible' =&gt; true,\r\n            'required' =&gt; false,\r\n            'user_defined' =&gt; false,\r\n            'default' =&gt; '',\r\n            'searchable' =&gt; false,\r\n            'filterable' =&gt; false,\r\n            'comparable' =&gt; false,\r\n            'unique' =&gt; false,\r\n            'visible_on_front' =&gt; false,\r\n            'used_in_product_listing' =&gt; true,\r\n            'apply_to' =&gt; ''\r\n        ]);\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>We have created the three custom product attributes.<\/p>\n<p>Custom Text Field -&gt; Text Box<br \/>\nCustom Yes\/No -&gt; Yes\/No Field<br \/>\nCustom Dropdown -&gt; Select Box<\/p>\n<p>&#8220;Custom Dropdown&#8221; is a select box and we have defined a custom source: Ht\\Mymodule\\Model\\Source\\Customdropdown<\/p>\n<p>So, we need to create the source file as well.<\/p>\n<p>File path: app\/code\/Ht\/Mymodule\/Model\/Source\/Customdropdown.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Model\\Source;\r\n\r\nclass Customdropdown extends \\Magento\\Eav\\Model\\Entity\\Attribute\\Source\\AbstractSource \r\n{\r\n    public function getAllOptions() {\r\n        if ($this-&gt;_options === null) {\r\n            $this-&gt;_options = [\r\n                ['label' =&gt; __('--Select--'), 'value' =&gt; ''],\r\n                ['label' =&gt; __('Option 1'), 'value' =&gt; 1],\r\n                ['label' =&gt; __('Option 2'), 'value' =&gt; 2]\r\n            ];\r\n        }\r\n        return $this-&gt;_options;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>At Final, Open terminal\/SSH and navigate to Magento 2 setup root directory and run below commands.<\/p>\n<pre class=\"lang:default decode:true \">php bin\/magento setup:upgrade\r\nphp bin\/magento setup:static-content:deploy -f<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7261\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-product-attributes.png\" alt=\"Magento 2 - How to Add Product Attribute Programmatically\" width=\"1474\" height=\"329\" srcset=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-product-attributes.png 1474w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-product-attributes-300x67.png 300w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-product-attributes-768x171.png 768w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-product-attributes-1024x229.png 1024w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-product-attributes-800x179.png 800w\" sizes=\"(max-width: 1474px) 100vw, 1474px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to add product attribute programmatically in magento 2. We are going to create three custom product attributes. Yes\/No, Select Option and Text field. We need to create the data setup script(InstallData.php) with parameters of the new attribute. File path: app\/code\/Ht\/Mymodule\/Setup\/InstallData.php &lt;?php namespace Ht\\Mymodule\\Setup; use Magento\\Eav\\Setup\\EavSetup; use&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-product-attribute-programmatically\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7333,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[9,10,683],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7260"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7260"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7260\/revisions"}],"predecessor-version":[{"id":7262,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7260\/revisions\/7262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7333"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}