During Magento customization, many of our clients request to extend the default functionality of Magento 2 categories. Recently one of them asked to add custom image attribute in category of Magento 2. To fulfill the requirement, I develop below code which I would like to share with you.
You need to add custom image attribute in the following files:
app/code/Vendor/Categoryattribute/Setup/ UpgradeData.php
First of all we need to add the attribute with proper parameters. Use code below:
If you have properly followed the code, image attribute would have been added to category in Magento 2 category.
Now once we have created the attribute, we need to save it in backend. Use following code:
Hope you followed the code properly and could create custom image attribute. If you are stuck between somewhere, I’m always there to provide you with solution. Comment your queries and suggestions.
Enjoy Coding!
Hi!
Thanks for a great article!
Is it possible to modify the code a bit in order to add custom file upload attribute to Products in Magento2? I want to be able to upload product sheets to products in Magento2.
Thanks in advance!
This tutorial is about adding custom image attribute in categories and not in products. To get the solution for adding image attribute in products, Contact us and we will be happy to help you further.
Hello,
Can you please tell me where we can add below code. Please give me file location.
<?php
namespace Vendor\Categoryattribute\Model\Category\Attribute\Backend;
………….
You need to create file at following location
app/code/Vendor\Categoryattribute\Model\Category\Attribute\Backend\Thumb.php and put the given code there.
Field not showing on backed. Where it will appear ?
The attribute should be available in “General” section, if you followed all the steps and executed it successfully.
hello attribute not showing in admin section
Can you please share the code which you have tried.
Hi,
There is no General information tab is available in Magento 2 Categories section?
So where added category attribute shown by using above code?
General Section means default first section when you open a category in admin.
I disappointed. Why did you post code that doesn’t work?
I see new attribute in eav_attribute table but I doesn’t see in admin panel.
Can you send us the code which you have tried, in case you have modified above code.
Send code on our email support@magecomp.com
I can not find where the field is added, what “general” section do you mean? I can not see a section with that name
Thanks
It’s the first tab in the category.
Hello
Where we can add “Vendor\Categoryattribute\CategoryThumbUpload” and can you please give me the example that file
Replace that thing with your actual third-party extension path.