Menu Close
    Hire us Request quote Reading list Switch to dark mode
    lockdownWebkulUp

    Add Custom Image Attribute To Category In Magento 2

    Add Custom Image Attribute To Category In Magento 2 – Here I’m going to explain you how to add custom image attribute to category.

    Step:-1

    Create a InstallData.php file on location vendor\Module\Setup


     

    Step:-2

    Create a category_form.xml file on location vendor\Module\view\adminhtml\ui_component


    Step:-3

    Add below code in Vendor/Module/etc/di.xml

    . . .
    Subscribe Articles

    Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

    You need to accept conditions to receive letters.
    You've Subscribed!

    Share article via

    . . .


    Step:-4

    Create Upload.php file on location Vendor/Module/Controller/Adminhtml/Category/Image


    Step:-5

    Then at last step create DataProvider.php file on location Vendor\Module\Model\Category


    In this way we can add image attribute to category. Thanks…

    . . .

    Comments (20)

    Add Your Comment

  • Samir
    can you provide route.xml code?
  • Mprasad
    Hi
    How to create category multislect attribute with search box
  • Eran Greenwald
    Hello
    Thank you for this post, it helped me a lot.
    When i upload an image i get 404 on this url:
    Site_Admin_Url/module/category_image/upload/key
    How can i fix this.

    Thanks 🙂

    • Huy Tran
      Just try add routes.xml into di/adminhtml.
      Replace module with your routes frontName.
      Your dir will separate with “_”: if your dir is Adminhtml/Category/Imageabc/Upload.php -> category_imageabc/upload
  • Sony
    Hi Rani,
    What was the Magento version when you did this tutorial ?
    I followed your steps on a Magento version 2.3 , but no luck.

    Actually all is working fine, except that the image is not saved.
    I think that we need to specify a custom backend source image which extends the native.
    Regards.

    • Rani Priya
      Hi Sony,

      Do you have created route for adminhtml? I mean etc/adminhtml/routes.xml file

      • Sony
        Hi yeah, i created the route. Weirdly it works now, do not know what change i made. But it seems working now, and at the end i used the native magento for the backend model ‘Magento\Catalog\Model\Category\Attribute\Backend\Image’, i did not need to create a custom backend model. Thanks ! 😉
  • Akshay
    Can you please explain.
    How to fetch this image in image src in frontend?
  • KW
    How do I install the module?
  • Fanboy
    A really good tutorial – could you add the part about creating the admin route so that this url part works.

  • sumeet
    Thanks,It is working fine.

    Thank you so much.

    • KW
      How do i enable or install the module after following the steps? Thanks!
      • Tom
        Run the “php bin/magento setup:upgrade” command 🙂
        • BilsTech
          php bin/magento cache:clear
  • Orvin Olson
    Not working,
    why you always not fill the full information ?
    getting this error – customcategorybannerimage/category_image/upload/key/a26831343598a8889c6c149f2976140ece250c41d2d4927c90117fd5a74bfab0/?isAjax=true

    and we know you not accept our comment

    • Rani Priya
      Hi Orvin,

      Hope it’s working fine at your end as well.

      In case if you will face any other issue then comment below we will surely help you.

  • Daniel Bello
    Hello, good job.
    How can we show this attribute on front end?
    Thanks
    • Rani Priya
      Hi Daniel,

      Please follow below steps in order to get image url at frontend.
      careate instance of class Magento\Catalog\Model\CategoryRepository
      then get category model by calling get method like
      $category = $categoryRepository->get($categoryId, $storeId);
      $imageUrl = “magento_media_url_path”.”catalog/category/”.$category->getCustomImage();
      And then you can use this $imageUrl in your custom category page or as per your design.

      Thanks

      • Karol
        any chance you write more specific in frontend part ? i cannot make this working ;(
        • Simon
          $this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . ‘catalog/category/’ . $category->getData(‘custom_image’);
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close