{"id":323,"date":"2019-02-22T10:49:39","date_gmt":"2019-02-22T10:49:39","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/02\/22\/create-product-category-cms-page-attributes-in-magento-2\/"},"modified":"2025-05-22T17:05:01","modified_gmt":"2025-05-22T11:35:01","slug":"create-product-category-cms-page-attributes-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-product-category-cms-page-attributes-in-magento-2\/","title":{"rendered":"How to Create Product, Category and CMS Page Attributes in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">An attribute is a property of products that can be used in various Magento 2 store\u2019s function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While creating extensions, developers may have the requirement to&nbsp;<em><strong>create product, category and CMS page attributes in Magento 2<\/strong><\/em>. Also, the product attributes help visitors find the most suitable products according to their requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 2 has predefined attributes like&nbsp;<em>Name<\/em>,&nbsp;<em>Price<\/em>, and&nbsp;<em>Description<\/em>. However, to improve the search functionality and allow the users to compare the products, use the custom product attributes!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Create Product, Category and CMS Page Attributes in Magento 2:<br><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Place the below code at&nbsp;<strong><strong>[Vendor]\\[Module]\\Setup\\InstallData.php<\/strong><\/strong><br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n \nnamespace [Vendor]\\[Module]\\Setup;\n \nuse Magento\\Framework\\Setup\\InstallDataInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Eav\\Setup\\EavSetupFactory;\nuse Magento\\Catalog\\Model\\Product;\nuse Magento\\Eav\\Model\\Entity\\Attribute\\ScopedAttributeInterface;\nuse Magento\\Catalog\\Model\\Category;\nuse Magento\\Framework\\DB\\Ddl\\Table;\n \nclass InstallData implements InstallDataInterface\n{\n    private $eavSetupFactory;\n \n    public function __construct(EavSetupFactory $eavSetupFactory)\n    {\n        $this->eavSetupFactory = $eavSetupFactory;\n    }\n \n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $installer = $setup;\n        $installer->startSetup();\n        $eavSetup = $this->eavSetupFactory->create();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>To Create Product Attribute:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$eavSetup->addAttribute(\n            Product::ENTITY,\n            'attribute_name',\n            [\n                'group' => 'group_name',\n                'type' => 'text',\n                'label' => 'Label',\n                'input' => 'text',\n                'required' => false,   \/\/ true if required\n                'global' => ScopedAttributeInterface::SCOPE_STORE,\n                'visible' => true,\n                'visible_on_front' => true\n            ]\n        );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check another method to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-product-attribute-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">create product attribute<\/a>&nbsp;from our blog. After done with this you can&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-custom-order-attribute-in-magento-2-and-show-it-in-admin-grid\/\">create custom order in Magento 2<\/a>&nbsp;which will be later seen in admin grid and will be helpful for your business in managing and fulfill the online orders efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>To Create Category Attribute:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$eavSetup->addAttribute(\n            Category::ENTITY,\n            'attribute_name',\n            [\n                'group' => 'group_name',\n                'type' => 'text',   \/\/ varchar,etc..\n                'label' => 'Label',\n                'input' => 'text',\n                'required' => false,\n                'global' => ScopedAttributeInterface::SCOPE_STORE,\n                'visible' => true,\n                'visible_on_front' => true\n            ]\n        );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>To Create CMS Page Attribute:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$setup->getConnection()->addColumn(\n            $setup->getTable('cms_page'),\n            'attribute_name',\n            [\n                'type' => Table::TYPE_TEXT,  \/\/ assign type as you need\n                'length' => '255',\n                'nullable' => true, \/\/ if required false\n                'comment' => 'comment text'\n            ]\n        );\n        \n        $installer->endSetup();\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Method to Remove Product or Category Attribute:<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);\n        $eavSetup->removeAttribute(\n          \\Magento\\Catalog\\Model\\Product::ENTITY,\n           'attribute_name');\n           \n           $eavSetup->removeAttribute(\n          \\Magento\\Catalog\\Model\\Category::ENTITY,\n           'attribute_name');\n           \n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Display the products more attractively with the help of the custom attributes that can be easily created with the help of the above method! Likewise you can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/programmatically-set-product-position-in-a-category-in-magento-2\/\">programmatically set product position in a category in Magento 2<\/a>&nbsp;as the product position value determines the order of products on the category page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An attribute is a property of products that can be used in various Magento 2 store\u2019s function. While creating extensions, developers may have the requirement&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":15504,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions\/15504"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}