{"id":12117,"date":"2016-12-20T19:18:11","date_gmt":"2016-12-20T16:18:11","guid":{"rendered":"http:\/\/blog.belvg.com\/?p=12117"},"modified":"2019-12-18T08:22:46","modified_gmt":"2019-12-18T06:22:46","slug":"how-to-add-image-attribute-to-a-category-in-magento-2","status":"publish","type":"post","link":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html","title":{"rendered":"How to Add Image Attribute to Category in Magento 2"},"content":{"rendered":"\n<p>It&rsquo;s much easier to add &ldquo;Image&rdquo; attribute during the upgrade or installation process of your module in the following files.<\/p>\n<p><!--more--><\/p>\n<p>app\/code\/<em>[ Vendor ]\/[ Module ]<\/em>\/Setup\/InstallData.php<\/p>\n<p>Or here:<\/p>\n<p>app\/code\/<em>[ Vendor ]\/[ Module ]<\/em>\/Setup\/UpgradeData.php<\/p>\n<h3>Let me show you how to perform it during upgrade<\/h3>\n<pre class=\"lang:php decode:true \">public function __construct(\\Magento\\Eav\\Setup\\EavSetupFactory $eavSetupFactory)\r\n{\r\n        $this-&gt;eavSetupFactory = $eavSetupFactory;\r\n}\r\n \r\npublic function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\r\n{\r\n        $setup-&gt;startSetup();\r\n \r\n    \tif (version_compare($context-&gt;getVersion(), '9.0.1', '&lt;')) {\r\n            $eavSetup = $this-&gt;eavSetupFactory-&gt;create(['setup' =&gt; $setup]);\r\n \r\n            $eavSetup-&gt;addAttribute(\r\n                \\Magento\\Catalog\\Model\\Category::ENTITY,\r\n                'image_thumb', [\r\n                    'type'      \t=&gt; 'varchar',\r\n                    'label'      \t=&gt; 'Image - Thumb',\r\n                    'input'     \t=&gt; 'image',\r\n                    'required' \t=&gt; false,\r\n                    'sort_order'  =&gt; 6,\r\n                    'backend'\t=&gt; '[ Vendor ]\\[ Module ]\\Model\\Category\\Attribute\\Backend\\Thumb',\r\n                    'global'    \t=&gt; \\Magento\\Eav\\Model\\Entity\\Attribute\\ScopedAttributeInterface::SCOPE_STORE,\r\n                    'group'    \t=&gt; 'General Information',\r\n                ]\r\n        \t);\r\n    \t}\r\n \r\n        $setup-&gt;endSetup();\r\n}\r\n<\/pre>\n<p>The second step you have to do is to add this attribute to a category form:<\/p>\n<p>app\/code\/<em>[ Vendor ]\/[ Module ]<\/em>\/view\/adminhtml\/ui_component\/category_form.xml<\/p>\n<pre class=\"lang:xml decode:true \">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;form xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Ui:etc\/ui_configuration.xsd\"&gt;\r\n\t&lt;fieldset name=\"content\"&gt;\r\n        &lt;argument name=\"data\" xsi:type=\"array\"&gt;\r\n            &lt;item name=\"config\" xsi:type=\"array\"&gt;\r\n                &lt;item name=\"label\" xsi:type=\"string\" translate=\"true\"&gt;Content&lt;\/item&gt;\r\n                &lt;item name=\"collapsible\" xsi:type=\"boolean\"&gt;true&lt;\/item&gt;\r\n                &lt;item name=\"sortOrder\" xsi:type=\"number\"&gt;99&lt;\/item&gt;\r\n            &lt;\/item&gt;\r\n        &lt;\/argument&gt;\r\n        &lt;field name=\"image_thumb\"&gt;\r\n            &lt;argument name=\"data\" xsi:type=\"array\"&gt;\r\n                &lt;item name=\"config\" xsi:type=\"array\"&gt;\r\n                    &lt;item name=\"dataType\" xsi:type=\"string\"&gt;string&lt;\/item&gt;\r\n                    &lt;item name=\"source\" xsi:type=\"string\"&gt;category&lt;\/item&gt;\r\n                    &lt;item name=\"label\" xsi:type=\"string\" translate=\"true\"&gt;Image - Thumb&lt;\/item&gt;\r\n                    &lt;item name=\"visible\" xsi:type=\"boolean\"&gt;true&lt;\/item&gt;\r\n                    &lt;item name=\"formElement\" xsi:type=\"string\"&gt;fileUploader&lt;\/item&gt;\r\n                    &lt;item name=\"elementTmpl\" xsi:type=\"string\"&gt;ui\/form\/element\/uploader\/uploader&lt;\/item&gt;\r\n                    &lt;item name=\"previewTmpl\" xsi:type=\"string\"&gt;Magento_Catalog\/image-preview&lt;\/item&gt;\r\n                    &lt;item name=\"required\" xsi:type=\"boolean\"&gt;false&lt;\/item&gt;\r\n \t               &lt;item name=\"sortOrder\" xsi:type=\"number\"&gt;40&lt;\/item&gt;\r\n                    &lt;item name=\"uploaderConfig\" xsi:type=\"array\"&gt;\r\n                        &lt;item name=\"url\" xsi:type=\"url\" path=\"[ module ]\/category_thumb\/upload\"\/&gt;\r\n                    &lt;\/item&gt;\r\n                &lt;\/item&gt;\r\n            &lt;\/argument&gt;\r\n        &lt;\/field&gt;\r\n    &lt;\/fieldset&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p>It would be enough for regular attributes, but not in case of the uploaded &ldquo;image&rdquo; attribute. Let&rsquo;s find out what else we should do to add this attribute properly.<\/p>\n<p>We have already defined the backend model:<\/p>\n<pre class=\"lang:php decode:true \">'backend' =&gt; '[ Vendor ]\\[ Module ]\\Model\\Category\\Attribute\\Backend\\Thumb',<\/pre>\n<p>And we have defined another URL for the upload:<\/p>\n<pre class=\"lang:xml decode:true \">&lt;item name=\"url\" xsi:type=\"url\" path=\"[ module ]\/category_thumb\/upload\"\/&gt;<\/pre>\n<p>Probably you are wondering, why do we need another Controller and a new Model? Why can&rsquo;t we use the default controller and model, that is already defined in the &ldquo;Image&rdquo; attribute:<\/p>\n<p><em>vendor\/magento\/module-catalog\/Model\/Category\/Attribute\/Backend\/Image.php<\/em><\/p>\n<p><em>vendor\/magento\/module-catalog\/Controller\/Adminhtml\/Category\/Image\/Upload.php<\/em><\/p>\n<div class=\"module_in_content_container\"><a href=\"https:\/\/belvg.com\/magento-audit\" class=\"link_to_module\" target=\"_blank\" rel=\"noopener\"><\/a><div class=\"module_content\"><div class=\"module_content_part\"><h3 class=\"title\">Magento Audit<\/h3><p class=\"module_content_text\">Take your online store to the next level with BelVG Magento Audit<\/p><a href=\"https:\/\/belvg.com\/magento-audit\" class=\"link_2_to_module\" target=\"_blank\" style=\"display:inline-block\" rel=\"noopener\"><span class=\"read-more\">Visit the page<\/span><\/a><\/div><\/div><\/div>\n<p>The answer is pretty simple: Magento 2.1.2 still has some versatility issues and code of attribute is placed right in the files. But we have our own attribute, that&rsquo;s why we should create a new one:<\/p>\n<p><em>app\/code\/[ Vendor ]\/[ Module ]\/Controller\/Adminhtml\/Category\/Thumb\/Upload.php<\/em><\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\nnamespace [ Vendor ]\\[ Module ]\\Controller\\Adminhtml\\Category\\Thumb;\r\nuse Magento\\Framework\\Controller\\ResultFactory;\r\n \r\nclass Upload extends \\Magento\\Backend\\App\\Action\r\n{\r\n\tprotected $imageUploader;\r\n \r\n\tpublic function __construct(\r\n        \\Magento\\Backend\\App\\Action\\Context $context,\r\n        \\Magento\\Catalog\\Model\\ImageUploader $imageUploader\r\n\t) {\r\n        parent::__construct($context);\r\n        $this-&gt;imageUploader = $imageUploader;\r\n\t}\r\n \r\n\tprotected function _isAllowed()\r\n\t{\r\n    \treturn $this-&gt;_authorization-&gt;isAllowed('Magento_Catalog::categories');\r\n\t}\r\n \r\n\tpublic function execute()\r\n\t{\r\n    \ttry {\r\n            $result = $this-&gt;imageUploader-&gt;saveFileToTmpDir('image_thumb');\r\n \r\n            $result['cookie'] = [\r\n                'name' \t=&gt; $this-&gt;_getSession()-&gt;getName(),\r\n                'value' \t=&gt; $this-&gt;_getSession()-&gt;getSessionId(),\r\n                'lifetime'   =&gt; $this-&gt;_getSession()-&gt;getCookieLifetime(),\r\n           \t 'path'       =&gt; $this-&gt;_getSession()-&gt;getCookiePath(),\r\n                'domain'   =&gt; $this-&gt;_getSession()-&gt;getCookieDomain(),\r\n        \t];\r\n    \t} catch (\\Exception $e) {\r\n            $result = ['error' =&gt; $e-&gt;getMessage(), 'errorcode' =&gt; $e-&gt;getCode()];\r\n    \t}\r\n    \treturn $this-&gt;resultFactory-&gt;create(ResultFactory::TYPE_JSON)-&gt;setData($result);\r\n\t}\r\n}\r\n<\/pre>\n<p>As you can see, the most important thing here is to specify the field in which the uploaded file <em>$this-&gt;imageUploader-&gt;saveFileToTmpDir(&lsquo;image_thumb&rsquo;)<\/em> should be searched.<\/p>\n<p>This Controller uploads an image to a temporary directory with the AJAX request. And shows its thumbnail in the admin panel even before the category has been saved.<\/p>\n<p>Further, we need to define a model, which allows us to save the uploaded file, but now during the category saving process.<\/p>\n<p><em>app\/code\/[ Vendor ]\/[ Module ]\/Model\/Category\/Attribute\/Backend\/Thumb.php<\/em><\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\nnamespace [ Vendor ]\\[ Module ]\\Model\\Category\\Attribute\\Backend;\r\n \r\nclass Thumb extends \\Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend\r\n{\r\n\tprotected $_uploaderFactory;\r\n\tprotected $_filesystem;\r\n\tprotected $_fileUploaderFactory;\r\n\tprotected $_logger;\r\n\tprivate $imageUploader;\r\n \r\n\tpublic function __construct(\r\n        \\Psr\\Log\\LoggerInterface $logger,\r\n        \\Magento\\Framework\\Filesystem $filesystem,\r\n    \t\\Magento\\MediaStorage\\Model\\File\\UploaderFactory $fileUploaderFactory\r\n\t) {\r\n        $this-&gt;_filesystem                = $filesystem;\r\n        $this-&gt;_fileUploaderFactory = $fileUploaderFactory;\r\n        $this-&gt;_logger                      = $logger;\r\n\t}\r\n \r\n    private function getImageUploader()\r\n\t{\r\n    \tif ($this-&gt;imageUploader === NULL) {\r\n            $this-&gt;imageUploader = \\Magento\\Framework\\App\\ObjectManager::getInstance()-&gt;get(\r\n                '[ Vendor ]\\[ Module ]\\CategoryThumbUpload'\r\n        \t);\r\n    \t}\r\n    \treturn $this-&gt;imageUploader;\r\n\t}\r\n \r\n\tpublic function beforeSave($object)\r\n\t{\r\n        $attrCode = $this-&gt;getAttribute()-&gt;getAttributeCode();\r\n \r\n    \tif (!$object-&gt;hasData($attrCode)) {\r\n            $object-&gt;setData($attrCode, NULL);\r\n    \t} else {\r\n            $values = $object-&gt;getData($attrCode);\r\n        \tif (is_array($values)) {\r\n                if (!empty($values['delete'])) {\r\n                    $object-&gt;setData($attrCode, NULL);\r\n                } else {\r\n                    if (isset($values[0]['name']) &amp;&amp; isset($values[0]['tmp_name'])) {\r\n                        $object-&gt;setData($attrCode, $values[0]['name']);\r\n                    } else {\r\n                        \/\/ don't update\r\n                    }\r\n                }\r\n      \t  }\r\n    \t}\r\n \r\n    \treturn $this;\r\n\t}\r\n \r\n\tpublic function afterSave($object)\r\n\t{\r\n    \t$image = $object-&gt;getData($this-&gt;getAttribute()-&gt;getName(), NULL);\r\n \r\n    \tif ($image !== NULL) {\r\n        \ttry {\r\n                $this-&gt;getImageUploader()-&gt;moveFileFromTmp($image);\r\n        \t} catch (\\Exception $e) {\r\n                $this-&gt;_logger-&gt;critical($e);\r\n        \t}\r\n    \t}\r\n \r\n    \treturn $this;\r\n\t}\r\n}\r\n<\/pre>\n<p>After the AJAX request, imageUploader returns not only an image itself, but an array with the updated data for the category object. And we should write another string to add the attribute to <em>varchar<\/em>.<\/p>\n<p><strong>beforeSave<\/strong> &ndash; transforms received array to a file name.<\/p>\n<p><strong>afterSave<\/strong> &ndash; transfers a file from temporary category to a constant one.<\/p>\n<p>But there is more to come. We haven&rsquo;t defined those file types, that could be uploaded. And those categories in which should use the uploaded image.<\/p>\n<p>However, we already specified the backend model for these configurations. They are defined in the<br>\n<em>app\/code\/[ Vendor ]\/[ Module ]\/etc\/di.xml<\/em><\/p>\n<pre class=\"lang:xml decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n \r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\"&gt;\r\n    &lt;virtualType name=\"[ Vendor ]\\[ Module ]\\CategoryThumbUpload\" type=\"Magento\\Catalog\\Model\\ImageUploader\"&gt;\r\n        &lt;arguments&gt;\r\n            &lt;argument name=\"baseTmpPath\" xsi:type=\"string\"&gt;catalog\/tmp\/category&lt;\/argument&gt;\r\n            &lt;argument name=\"basePath\" xsi:type=\"string\"&gt;catalog\/category&lt;\/argument&gt;\r\n            &lt;argument name=\"allowedExtensions\" xsi:type=\"array\"&gt;\r\n                &lt;item name=\"jpg\" xsi:type=\"string\"&gt;jpg&lt;\/item&gt;\r\n                &lt;item name=\"jpeg\" xsi:type=\"string\"&gt;jpeg&lt;\/item&gt;\r\n                &lt;item name=\"gif\" xsi:type=\"string\"&gt;gif&lt;\/item&gt;\r\n                &lt;item name=\"png\" xsi:type=\"string\"&gt;png&lt;\/item&gt;\r\n            &lt;\/argument&gt;\r\n        &lt;\/arguments&gt;\r\n    &lt;\/virtualType&gt;\r\n\t&lt;type name=\"[ Vendor ]\\[ Module ]\\Controller\\Adminhtml\\Category\\Thumb\\Upload\"&gt;\r\n        &lt;arguments&gt;\r\n            &lt;argument name=\"imageUploader\" xsi:type=\"object\"&gt;[ Vendor ]\\[ Module ]\\CategoryThumbUpload&lt;\/argument&gt;\r\n        &lt;\/arguments&gt;\r\n    &lt;\/type&gt;\r\n    &lt;preference for=\"Magento\\Catalog\\Model\\Category\\DataProvider\" type=\"[ Vendor ]\\[ Module ]\\Model\\Category\\DataProvider\" \/&gt;\r\n&lt;\/config&gt;\r\n<\/pre>\n<p>It seems that we finally finished, but after image uploading and category saving admin panel is getting stucked.<\/p>\n<div class=\"module_in_content_container\"><a href=\"https:\/\/belvg.com\/magento-webdesign\" class=\"link_to_module\" target=\"_blank\" rel=\"noopener\"><\/a><div class=\"module_content\"><img src=\"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2019\/01\/magento-webdesign-1.png\" width=\"100\" height=\"100\" alt=\"magento webdesign\" class=\"module_image\" title=\"How to Add Image Attribute to Category in Magento 2\"><div class=\"module_content_part\"><h3 class=\"title\">Magento Webdesign<\/h3><p class=\"module_content_text\">Take your online store to the next level with BelVG Magento Webdesign<\/p><a href=\"https:\/\/belvg.com\/magento-webdesign\" class=\"link_2_to_module\" target=\"_blank\" style=\"display:inline-block\" rel=\"noopener\"><span class=\"read-more\">Visit the page<\/span><\/a><\/div><\/div><\/div>\n<p>Additionally we should define the rewrite for <strong>DataProvider<\/strong> file in the <strong>di.xml<\/strong>: <em>app\/code\/[ Vendor ]\/[ Module ]\/Model\/Category\/DataProvider.php<\/em><\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\nnamespace [ Vendor ]\\[ Module ]\\Model\\Category;\r\n \r\nclass DataProvider extends \\Magento\\Catalog\\Model\\Category\\DataProvider\r\n{\r\n\tprotected function addUseDefaultSettings($category, $categoryData)\r\n\t{\r\n    \t$data = parent::addUseDefaultSettings($category, $categoryData);\r\n \r\n    \tif (isset($data['image_thumb'])) {\r\n            unset($data['image_thumb']);\r\n \r\n            $objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n            $helper           \t= $objectManager-&gt;get('\\[ Vendor ]\\[ Module ]\\Helper\\Data');\r\n \r\n            $data['image_thumb'][0]['name'] = $category-&gt;getData('image_thumb');\r\n            $data['image_thumb'][0]['url']  \t= $helper-&gt;getCategoryThumbUrl($category);\r\n    \t}\r\n \r\n    \treturn $data;\r\n\t}\r\n \r\n\tprotected function getFieldsMap()\r\n\t{\r\n    \t$fields = parent::getFieldsMap();\r\n        $fields['content'][] = 'image_thumb'; \/\/ NEW FIELD\r\n    \t\r\n    \treturn $fields;\r\n\t}\r\n}\r\n<\/pre>\n<p>And in the end, we have to display this image on the frontend. There is only a file name in the category data. And the following function will help to create a full URL for it.<\/p>\n<pre class=\"lang:php decode:true \">public function getCategoryThumbUrl($category)\r\n{\r\n    $url   = false;\r\n    $image = $category-&gt;getImageThumb();\r\n    if ($image) {\r\n        if (is_string($image)) {\r\n            $url = $this-&gt;_storeManager-&gt;getStore()-&gt;getBaseUrl(\r\n                    \\Magento\\Framework\\UrlInterface::URL_TYPE_MEDIA\r\n            ) . 'catalog\/category\/' . $image;\r\n        } else {\r\n            throw new \\Magento\\Framework\\Exception\\LocalizedException(\r\n                    __('Something went wrong while getting the image url.')\r\n            );\r\n        }\r\n    }\r\n\r\n    return $url;\r\n}\r\n<\/pre>\n<h3>Check also:<\/h3>\n<p><a href=\"https:\/\/belvg.com\/blog\/adding-image-attribute-to-a-category-in-magento-2-alternative-version.html\">Adding Image Attribute To a Category in Magento 2: Alternative Version<\/a><\/p>\n<div class=\"module_in_content_container\"><a href=\"https:\/\/store.belvg.com\/customer-images-uploader.html\" class=\"link_to_module\" target=\"_blank\" rel=\"noopener\"><\/a><div class=\"module_content\"><img src=\"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2016\/12\/magento_ico_1.9_customer_images_uploader.jpg\" width=\"450\" height=\"450\" alt=\"magento_ico_1.9_customer_images_uploader\" class=\"module_image\" title=\"How to Add Image Attribute to Category in Magento 2\"><div class=\"module_content_part\"><h3 class=\"title\">Customer Images Uploader<\/h3><p class=\"module_content_text\">Take your online store to the new level with this quality Magento extension by BelVG<\/p><a href=\"https:\/\/store.belvg.com\/customer-images-uploader.html\" class=\"link_2_to_module\" target=\"_blank\" style=\"display:inline-block\" rel=\"noopener\"><span class=\"read-more\">Download here<\/span><\/a><\/div><\/div><\/div>\n<p>Need to improve product management mechanism and categories visibility? Check out BelVG&rsquo;s&nbsp;<a href=\"https:\/\/store.belvg.com\/groups-catalog.html\" target=\"_blank\" rel=\"noopener\">Groups Catalog for Magento 2<\/a> extension.<\/p>\n\n","protected":false},"excerpt":{"rendered":"It&#8217;s much easier to add &#8220;Image&#8221; attribute during the upgrade or installation process of your &#8230;","protected":false},"author":22,"featured_media":12118,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_acf_changed":false,"footnotes":""},"categories":[14,389,524],"tags":[],"class_list":["post-12117","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-magento","category-magento2-development","category-magento-speed-optimization","post_format-post-format-image"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"Find out how to add image attribute to the category in Magento 2. Learn how to add image attribute to the files during module upgrade or installation.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Mikhail Soiko\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#article\",\"name\":\"How to Add Image Attribute to Category in Magento | BelVG Blog\",\"headline\":\"How to Add Image Attribute to Category in Magento 2\",\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/mishell-soiko#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/12\\\/pimgpsh_fullsize_distr.jpg\",\"width\":770,\"height\":209,\"caption\":\"How to Add \\u201cImage\\u201d Attribute to a Category in Magento 2\"},\"datePublished\":\"2016-12-20T19:18:11+02:00\",\"dateModified\":\"2019-12-18T08:22:46+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":24,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#webpage\"},\"articleSection\":\"Magento, Magento 2 Development, Magento Speed Optimization, Image\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento#listItem\",\"name\":\"Magento\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento#listItem\",\"position\":2,\"name\":\"Magento\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#listItem\",\"name\":\"How to Add Image Attribute to Category in Magento 2\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#listItem\",\"position\":3,\"name\":\"How to Add Image Attribute to Category in Magento 2\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento#listItem\",\"name\":\"Magento\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/mishell-soiko#author\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/mishell-soiko\",\"name\":\"Mikhail Soiko\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1603d45135617d53877de2d9ffe112a497830fa8a92f16dd13b43069b1e53482?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Mikhail Soiko\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#webpage\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html\",\"name\":\"How to Add Image Attribute to Category in Magento | BelVG Blog\",\"description\":\"Find out how to add image attribute to the category in Magento 2. Learn how to add image attribute to the files during module upgrade or installation.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/mishell-soiko#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/mishell-soiko#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/12\\\/pimgpsh_fullsize_distr.jpg\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html\\\/#mainImage\",\"width\":770,\"height\":209,\"caption\":\"How to Add \\u201cImage\\u201d Attribute to a Category in Magento 2\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-add-image-attribute-to-a-category-in-magento-2.html#mainImage\"},\"datePublished\":\"2016-12-20T19:18:11+02:00\",\"dateModified\":\"2019-12-18T08:22:46+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"How to Add Image Attribute to Category in Magento | BelVG Blog","description":"Find out how to add image attribute to the category in Magento 2. Learn how to add image attribute to the files during module upgrade or installation.","canonical_url":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#article","name":"How to Add Image Attribute to Category in Magento | BelVG Blog","headline":"How to Add Image Attribute to Category in Magento 2","author":{"@id":"https:\/\/belvg.com\/blog\/author\/mishell-soiko#author"},"publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2016\/12\/pimgpsh_fullsize_distr.jpg","width":770,"height":209,"caption":"How to Add \u201cImage\u201d Attribute to a Category in Magento 2"},"datePublished":"2016-12-20T19:18:11+02:00","dateModified":"2019-12-18T08:22:46+02:00","inLanguage":"en-US","commentCount":24,"mainEntityOfPage":{"@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#webpage"},"isPartOf":{"@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#webpage"},"articleSection":"Magento, Magento 2 Development, Magento Speed Optimization, Image"},{"@type":"BreadcrumbList","@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/belvg.com\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento#listItem","name":"Magento"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento#listItem","position":2,"name":"Magento","item":"https:\/\/belvg.com\/blog\/category\/magento","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#listItem","name":"How to Add Image Attribute to Category in Magento 2"},"previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#listItem","position":3,"name":"How to Add Image Attribute to Category in Magento 2","previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento#listItem","name":"Magento"}}]},{"@type":"Organization","@id":"https:\/\/belvg.com\/blog\/#organization","name":"BelVG Blog","description":"Magento and Prestashop articles","url":"https:\/\/belvg.com\/blog\/"},{"@type":"Person","@id":"https:\/\/belvg.com\/blog\/author\/mishell-soiko#author","url":"https:\/\/belvg.com\/blog\/author\/mishell-soiko","name":"Mikhail Soiko","image":{"@type":"ImageObject","@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/1603d45135617d53877de2d9ffe112a497830fa8a92f16dd13b43069b1e53482?s=96&d=mm&r=g","width":96,"height":96,"caption":"Mikhail Soiko"}},{"@type":"WebPage","@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#webpage","url":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html","name":"How to Add Image Attribute to Category in Magento | BelVG Blog","description":"Find out how to add image attribute to the category in Magento 2. Learn how to add image attribute to the files during module upgrade or installation.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/belvg.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#breadcrumblist"},"author":{"@id":"https:\/\/belvg.com\/blog\/author\/mishell-soiko#author"},"creator":{"@id":"https:\/\/belvg.com\/blog\/author\/mishell-soiko#author"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2016\/12\/pimgpsh_fullsize_distr.jpg","@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html\/#mainImage","width":770,"height":209,"caption":"How to Add \u201cImage\u201d Attribute to a Category in Magento 2"},"primaryImageOfPage":{"@id":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html#mainImage"},"datePublished":"2016-12-20T19:18:11+02:00","dateModified":"2019-12-18T08:22:46+02:00"},{"@type":"WebSite","@id":"https:\/\/belvg.com\/blog\/#website","url":"https:\/\/belvg.com\/blog\/","name":"BelVG Blog","description":"Magento and Prestashop articles","inLanguage":"en-US","publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"}}]}},"aioseo_meta_data":{"post_id":"12117","title":"How to Add Image Attribute to Category in Magento | #site_title","description":"Find out how to add image attribute to the category in Magento 2. Learn how to add image attribute to the files during module upgrade or installation.","keywords":null,"keyphrases":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_custom_url":null,"og_image_custom_fields":null,"og_image_url":null,"og_image_width":null,"og_image_height":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_image_url":null,"twitter_title":null,"twitter_description":null,"schema_type":"default","schema_type_options":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null,"created":"2026-11-03 18:37:24","updated":"2026-11-03 18:37:24"},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/category\/magento\" title=\"Magento\">Magento<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tHow to Add Image Attribute to Category in Magento 2\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/belvg.com\/blog\/"},{"label":"Magento","link":"https:\/\/belvg.com\/blog\/category\/magento"},{"label":"How to Add Image Attribute to Category in Magento 2","link":"https:\/\/belvg.com\/blog\/how-to-add-image-attribute-to-a-category-in-magento-2.html"}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/12117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/comments?post=12117"}],"version-history":[{"count":1,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/12117\/revisions"}],"predecessor-version":[{"id":25286,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/12117\/revisions\/25286"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media\/12118"}],"wp:attachment":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media?parent=12117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/categories?post=12117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/tags?post=12117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}