Wednesday, January 4, 2012

Magento : Add custom image attribute to category

 Magento : Add custom image attribute to category

Just copy paste the below code in header.phtml and run yourmagento once, your attribute will be created and you can see in backend under manage category. After you are done remove this code again.

 ------------------------------------------------------------------------------------------
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('catalog_category', 'sliderimage', array(
    'group'         => 'General',
    'input'         => 'image',
    'type'          => 'varchar',
    'label'         => 'Slider Image',
    'backend'       => 'catalog/category_attribute_backend_image',
    'visible'       => 1,
    'required'        => 0,
    'user_defined' => 1,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
------------------------------------------------------------------------------------------

38 comments:

  1. Thanks afroz, it really helped me

    ReplyDelete
  2. Hi Great work there,

    Well how can we display this newly added attribute on the backend?

    ReplyDelete
    Replies
    1. The created attribute is automatically visible in backend

      Delete
  3. well it rly helped nd it work but how can i verify validation ? when i put required as 1 even on upload it say my atttribute is empty

    ReplyDelete
    Replies
    1. @kareem .. i cannot get your point . can you please mail me the issue with screenshot.

      Delete
  4. Thank you so much for this. after a complete waste of day this saved my ass!

    ReplyDelete
  5. in which file i use the above code.
    addAttribute('catalog_category', 'sliderimage', array(
    'group' => 'General',
    'input' => 'image',
    'type' => 'varchar',
    'label' => 'Slider Image',
    'backend' => 'catalog/category_attribute_backend_image',
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    ));

    ?>
    its not working for me

    ReplyDelete
  6. Guys, this is ridiculous. You don't even know what an sql update is.
    The code is correct, but putting it into header and then removing - nothing to say.

    ReplyDelete
  7. How to display this category image on the frontend?

    ReplyDelete
    Replies
    1. I dont know it neither. I've tried everything..

      Delete
    2. getStoreCategories();

      foreach ($storeCategories as $_category):
      $_categoryModel = Mage::getModel('catalog/category')->load($_category->getId());
      $op = $_categoryModel->getData('sliderimage');
      echo Mage::getBaseUrl('media').'catalog/category/'.$op;
      endforeach;
      ?>

      Delete
  8. How to create more than 1 image fields.

    ReplyDelete
  9. How can I call it? getSliderimage();?> doesn't work.

    ReplyDelete
    Replies
    1. getStoreCategories();

      foreach ($storeCategories as $_category):
      $_categoryModel = Mage::getModel('catalog/category')->load($_category->getId());
      $op = $_categoryModel->getData('sliderimage');
      echo Mage::getBaseUrl('media').'catalog/category/'.$op;
      endforeach;
      ?>

      Delete
  10. I found this post searching for the way to show the category image on the frontend, looking at the last comments I think I'm probably not the only one. The answer is: reindex. Then the image filename will be available with ->getData('sliderimage') on the frontend.

    ReplyDelete
  11. $setup = new Mage_Eav_Model_Entity_Setup('core_setup');

    $setup->addAttribute('catalog_category', 'sliderimage', array(
    'group' => 'General',
    'input' => 'image',
    'type' => 'varchar',
    'label' => 'Slider Image',
    'backend' => 'catalog/category_attribute_backend_image',
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    ));
    Hey Thanks for providing code
    Its working in localhost but in server not displaying this field.

    ReplyDelete
  12. when i trying to get the attr value using getData it return the file name only, how can i retrieve the full image url or generate it ?

    ReplyDelete
    Replies
    1. You can concatenate the media_url and filename to get the end result.

      Delete
  13. Thanks for this code. Its really amazing working..

    ReplyDelete
    Replies
    1. Thanks for your comments. Follow my blog to get notifications as soon as add new posts.

      Delete
  14. Great Read! I am impressed on how you make your article easy to understand. I'll come back for more :D

    Japs Buidon is a Social Media Specialist and belongs to a team of Magento Developer in Florida. He loves hiking as well as electronics.

    ReplyDelete
  15. hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.
    Kindly keep blogging. If anyone wants to become a Front end developer learn from javascript and jquery training in chennai .
    or learn thru Javascript Training in Chennai.
    Nowadays JavaScript has tons of job opportunities on various vertical industry. javascript and jquery training in chennai

    ReplyDelete
  16. I am definitely enjoying your website. You definitely have some great insight and great stories. 
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

    ReplyDelete