{"id":373,"date":"2016-02-29T22:40:33","date_gmt":"2016-02-29T17:10:33","guid":{"rendered":"http:\/\/www.magewallet.com\/?p=373"},"modified":"2020-03-13T16:08:26","modified_gmt":"2020-03-13T10:38:26","slug":"adding-new-category-attribute-in-magento-2","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/adding-new-category-attribute-in-magento-2\/","title":{"rendered":"Adding new category attribute in Magento 2"},"content":{"rendered":"<p>Sometimes you need to extend functionality of Magento 2 categories. In this article I would like to show you how to add a new custom category attribute. Let\u2019s say, this attribute is needed to display\/hide some content on the category page.<\/p>\n<p>Now let&#8217;s write our installation script under Setup folder. I&#8217;ve included comments within to guide you along. Note that the custom variable I&#8217;m adding is called hide_this_category. This attribute will be used for hide particular category from menu.<\/p>\n<p>In this case I&#8217;ve added a Boolean field that will be stored in used as Yes\/No, but you can change this according to your requirement.<\/p>\n<p>Lets check code to add custom category attribute.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Hiddentechies\\CategoryAttr\\Setup;\r\n\r\nuse Magento\\Framework\\Module\\Setup\\Migration;\r\nuse Magento\\Framework\\Setup\\InstallDataInterface;\r\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\r\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\r\nuse Magento\\Catalog\\Setup\\CategorySetupFactory;\r\n\r\n\/**\r\n * @codeCoverageIgnore\r\n *\/\r\nclass InstallData implements InstallDataInterface {\r\n\r\n    \/**\r\n     * Category setup factory\r\n     *\r\n     * @var CategorySetupFactory\r\n     *\/\r\n    private $categorySetupFactory;\r\n\r\n    \/**\r\n     * Init\r\n     *\r\n     * @param CategorySetupFactory $categorySetupFactory\r\n     *\/\r\n    public function __construct(CategorySetupFactory $categorySetupFactory) {\r\n        $this-&gt;categorySetupFactory = $categorySetupFactory;\r\n    }\r\n\r\n    \/**\r\n     * {@inheritdoc}\r\n     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)\r\n     *\/\r\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {\r\n        $installer = $setup;\r\n\r\n        $installer-&gt;startSetup();\r\n\r\n        $categorySetup = $this-&gt;categorySetupFactory-&gt;create(['setup' =&gt; $setup]);\r\n        $entityTypeId = $categorySetup-&gt;getEntityTypeId(\\Magento\\Catalog\\Model\\Category::ENTITY);\r\n        $attributeSetId = $categorySetup-&gt;getDefaultAttributeSetId($entityTypeId);\r\n\r\n        $menu_attributes = [\r\n            'hide_this_category' =&gt; [\r\n                'type' =&gt; 'int',\r\n                'label' =&gt; 'Hide This Category',\r\n                'input' =&gt; 'select',\r\n                'source' =&gt; 'Magento\\Eav\\Model\\Entity\\Attribute\\Source\\Boolean',\r\n                'required' =&gt; false,\r\n                'sort_order' =&gt; 10,\r\n                'global' =&gt; \\Magento\\Eav\\Model\\Entity\\Attribute\\ScopedAttributeInterface::SCOPE_STORE,\r\n                'group' =&gt; 'Custom Attribute'\r\n            ]\r\n        ];\r\n\r\n        foreach ($menu_attributes as $item =&gt; $data) {\r\n            $categorySetup-&gt;addAttribute(\\Magento\\Catalog\\Model\\Category::ENTITY, $item, $data);\r\n        }\r\n\r\n        $idg = $categorySetup-&gt;getAttributeGroupId($entityTypeId, $attributeSetId, 'Custom Attribute');\r\n\r\n        foreach ($menu_attributes as $item =&gt; $data) {\r\n            $categorySetup-&gt;addAttributeToGroup(\r\n                    $entityTypeId, $attributeSetId, $idg, $item, $data['sort_order']\r\n            );\r\n        }\r\n\r\n        $installer-&gt;endSetup();\r\n    }\r\n\r\n}<\/pre>\n<p>Please setup this installation script with your extension and run upgrade command via command line. If things didn&#8217;t work out, check to see if the installation script actually ran and make sure that you cleared your cache and double-check your installation script filename, path and version number with Magento 2 extension structure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need to extend functionality of Magento 2 categories. In this article I would like to show you how to add a new custom category attribute. Let\u2019s say, this attribute is needed to display\/hide some content on the category page. Now let&#8217;s write our installation script under Setup folder. I&#8217;ve included comments within to&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/adding-new-category-attribute-in-magento-2\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":4882,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[84,85,10],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/373"}],"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=373"}],"version-history":[{"count":3,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/373\/revisions"}],"predecessor-version":[{"id":10505,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/373\/revisions\/10505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/4882"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}