{"id":3618,"date":"2022-12-01T06:15:21","date_gmt":"2022-12-01T06:15:21","guid":{"rendered":"https:\/\/www.cynoinfotech.com\/?p=3618"},"modified":"2022-12-01T06:17:11","modified_gmt":"2022-12-01T06:17:11","slug":"magento-2-how-to-create-custom-category-attribute","status":"publish","type":"post","link":"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/","title":{"rendered":"MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE"},"content":{"rendered":"<p>In this blog, we are going through how to add custom category attributes in Magento 2.<\/p>\n<p>Why Create a Custom Category Attribute in Magento 2? Because in a category you need to add some external text, numbers, multiple choice categories, etc. you can create category attribute<\/p>\n<p>In this blog, we create category attributes using the latest technology\u00a0 \u201c<strong>data\/patch<\/strong>\u201d.let\u2019s start with how to create categories attribute.<\/p>\n<p>\u21d2\u00a0 \u00a0 First of all, you create \u201c<strong>CategoryAttribute.php<\/strong>\u201d\u00a0 in this directory \u201c<strong>Vendor_name\/Module_name\/Setup\/Patch\/Data\/<\/strong>\u201d.<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\n\/**\n* @author CynoInfotech Team\n* @package vendor_module\n*\/\ndeclare (strict_types = 1);\nnamespace vendor\\module\\Setup\\Patch\\Data;\nuse Magento\\Eav\\Model\\Entity\\Attribute\\ScopedAttributeInterface;\nuse Magento\\Eav\\Setup\\EavSetup;\nuse Magento\\Eav\\Setup\\EavSetupFactory;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Framework\\Setup\\Patch\\DataPatchInterface;\nuse Magento\\Catalog\\Model\\Category;\n\/**\n * Class CategoryAttribute for Create Custom Category Attribute using Data Patch.\n *\/\nclass CategoryAttribute implements DataPatchInterface {\n    \/**\n     * ModuleDataSetupInterface\n     *\n     * @var ModuleDataSetupInterface\n     *\/\n    private $moduleDataSetup;\n    \/**\n     * EavSetupFactory\n     *\n     * @var EavSetupFactory\n     *\/\n    private $eavSetupFactory;\n    \/**\n     * @param ModuleDataSetupInterface $moduleDataSetup\n     * @param EavSetupFactory          $eavSetupFactory\n     *\/\n    public function __construct(\n        ModuleDataSetupInterface $moduleDataSetup,\n        EavSetupFactory $eavSetupFactory\n    ) {\n        $this-&gt;moduleDataSetup = $moduleDataSetup;\n        $this-&gt;eavSetupFactory = $eavSetupFactory;\n    }\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function apply() {\n        \/** @var EavSetup $eavSetup *\/\n        $eavSetup = $this-&gt;eavSetupFactory-&gt;create(['setup' =&gt; $this-&gt;moduleDataSetup]);\n        $eavSetup-&gt;addAttribute(Category::ENTITY, 'category_attribute', [\n            'type' =&gt; 'text',\n            'label' =&gt; 'Custom Category Attribute',\n            'input' =&gt; 'text',\n            'default' =&gt; 0,\n            'sort_order' =&gt; 5,\n            'global' =&gt; ScopedAttributeInterface::SCOPE_STORE,\n            'group' =&gt; 'General Information',\n            'visible_on_front' =&gt; true\n        ]);\n    }\n    \/**\n     * {@inheritdoc}\n     *\/\n    public static function getDependencies() {\n        return [];\n    }\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function getAliases() {\n        return [];\n    }\n}<\/pre>\n<p>\u21d2 After that you need to create &#8220;<strong>category_form.xml&#8221; <\/strong>in this directory &#8220;<strong>vendor_name\/module_name\/view\/adminhtml\/ui_component<\/strong>&#8220;.<\/p>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\" ?&gt;\n&lt;!--\n\/**\n* @author CynoInfotech Team\n* @package vendor_module\n*\/\n--&gt;\n&lt;form xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n    xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Ui:etc\/ui_configuration.xsd\"&gt;\n    &lt;fieldset name=\"general\"&gt;\n        &lt;field name=\"category_attribute\"&gt;\n            &lt;argument name=\"data\" xsi:type=\"array\"&gt;\n                &lt;item name=\"config\" xsi:type=\"array\"&gt;\n                    &lt;item name=\"required\" xsi:type=\"boolean\"&gt;false&lt;\/item&gt;\n                    &lt;item name=\"validation\" xsi:type=\"array\"&gt;\n                        &lt;item name=\"required-entry\" xsi:type=\"boolean\"&gt;false&lt;\/item&gt;\n                    &lt;\/item&gt;\n                    &lt;item name=\"sortOrder\" xsi:type=\"number\"&gt;100&lt;\/item&gt;\n                    &lt;item name=\"dataType\" xsi:type=\"string\"&gt;string&lt;\/item&gt;\n                    &lt;item name=\"formElement\" xsi:type=\"string\"&gt;input&lt;\/item&gt;\n                    &lt;item name=\"label\" translate=\"true\" xsi:type=\"string\"&gt;Custom Category Attribute&lt;\/item&gt;\n                &lt;\/item&gt;\n            &lt;\/argument&gt;\n        &lt;\/field&gt;\n    &lt;\/fieldset&gt;\n&lt;\/form&gt;<\/pre>\n<p>\u21d2 and last you need to run this command in the root directory<\/p>\n<ul>\n<li>php -dmemory_limit=2G bin\/magento setup:upgrade<\/li>\n<li>php -dmemory_limit=2G bin\/magento setup:static-content:deploy -f -j<\/li>\n<li>php -dmemory_limit=2G bin\/magento cache:flush<\/li>\n<\/ul>\n<p>\u21d2 after checking the custom category created successfully.\u00a0 If you have any questions regarding this blog let me know in the comment section.<\/p>\n<p>Thank You !!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we are going through how to add custom category attributes in Magento 2. Why Create a Custom Category Attribute in Magento 2? Because in a category you need to add some external text, numbers, multiple choice categories, etc. you can create category attribute In this blog, we create category attributes using the [&hellip;]<\/p>\n","protected":false},"author":670,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3618","post","type-post","status-publish","format-standard","hentry","category-magento-blog"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE - cynoinfotech<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/magento-2-how-to-create-custom-category-attribute\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/magento-2-how-to-create-custom-category-attribute\\\/\"},\"author\":{\"name\":\"Arjun Makadiya\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/person\\\/f1be8186602d3ebf417cf58baf125f24\"},\"headline\":\"MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE\",\"datePublished\":\"2022-12-01T06:15:21+00:00\",\"dateModified\":\"2022-12-01T06:17:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/magento-2-how-to-create-custom-category-attribute\\\/\"},\"wordCount\":173,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#organization\"},\"articleSection\":[\"Magento blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/magento-2-how-to-create-custom-category-attribute\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/magento-2-how-to-create-custom-category-attribute\\\/\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/magento-2-how-to-create-custom-category-attribute\\\/\",\"name\":\"MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE - cynoinfotech\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#website\"},\"datePublished\":\"2022-12-01T06:15:21+00:00\",\"dateModified\":\"2022-12-01T06:17:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/magento-2-how-to-create-custom-category-attribute\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/magento-2-how-to-create-custom-category-attribute\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/magento-2-how-to-create-custom-category-attribute\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/\",\"name\":\"cynoinfotech\",\"description\":\"web design &amp; development\",\"publisher\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#organization\",\"name\":\"Cynoinfotech\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.cynoinfotech.com\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/final-logo-ci.png\",\"contentUrl\":\"https:\\\/\\\/www.cynoinfotech.com\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/final-logo-ci.png\",\"width\":240,\"height\":70,\"caption\":\"Cynoinfotech\"},\"image\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/cynoinfotech\\\/\",\"https:\\\/\\\/x.com\\\/cynoinfotech\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/person\\\/f1be8186602d3ebf417cf58baf125f24\",\"name\":\"Arjun Makadiya\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g\",\"caption\":\"Arjun Makadiya\"},\"description\":\"Arjun Makadiya is a Junior Magento Developer at Cynoifotech. He is passionate about technology. Apart from work he likes to play cricket.\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/author\\\/arjun-makadiya\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE - cynoinfotech","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/#article","isPartOf":{"@id":"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/"},"author":{"name":"Arjun Makadiya","@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/person\/f1be8186602d3ebf417cf58baf125f24"},"headline":"MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE","datePublished":"2022-12-01T06:15:21+00:00","dateModified":"2022-12-01T06:17:11+00:00","mainEntityOfPage":{"@id":"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/"},"wordCount":173,"commentCount":4,"publisher":{"@id":"https:\/\/cynoinfotech.com\/blog\/#organization"},"articleSection":["Magento blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/","url":"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/","name":"MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE - cynoinfotech","isPartOf":{"@id":"https:\/\/cynoinfotech.com\/blog\/#website"},"datePublished":"2022-12-01T06:15:21+00:00","dateModified":"2022-12-01T06:17:11+00:00","breadcrumb":{"@id":"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cynoinfotech.com\/blog\/magento-2-how-to-create-custom-category-attribute\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cynoinfotech.com\/blog\/"},{"@type":"ListItem","position":2,"name":"MAGENTO 2 HOW TO CREATE CUSTOM CATEGORY ATTRIBUTE"}]},{"@type":"WebSite","@id":"https:\/\/cynoinfotech.com\/blog\/#website","url":"https:\/\/cynoinfotech.com\/blog\/","name":"cynoinfotech","description":"web design &amp; development","publisher":{"@id":"https:\/\/cynoinfotech.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cynoinfotech.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cynoinfotech.com\/blog\/#organization","name":"Cynoinfotech","url":"https:\/\/cynoinfotech.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.cynoinfotech.com\/wp-content\/uploads\/2017\/11\/final-logo-ci.png","contentUrl":"https:\/\/www.cynoinfotech.com\/wp-content\/uploads\/2017\/11\/final-logo-ci.png","width":240,"height":70,"caption":"Cynoinfotech"},"image":{"@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cynoinfotech\/","https:\/\/x.com\/cynoinfotech"]},{"@type":"Person","@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/person\/f1be8186602d3ebf417cf58baf125f24","name":"Arjun Makadiya","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g","caption":"Arjun Makadiya"},"description":"Arjun Makadiya is a Junior Magento Developer at Cynoifotech. He is passionate about technology. Apart from work he likes to play cricket.","url":"https:\/\/cynoinfotech.com\/blog\/author\/arjun-makadiya\/"}]}},"_links":{"self":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/3618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/users\/670"}],"replies":[{"embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=3618"}],"version-history":[{"count":2,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/3618\/revisions"}],"predecessor-version":[{"id":3621,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/3618\/revisions\/3621"}],"wp:attachment":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=3618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=3618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=3618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}