Blog

How to add a custom image field to a category in Magento?

Please follow below steps to add a new custom image attribute to add/edit category in magento back-end and call it in category view page at front-end.

Step 1:Disable Magento Cache from magento back-end.

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. Please remove this code once new attribute added.

 $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,
 ));

 

Step 2: Copy Category.php file from app/code/core/Mage/Catalog/Model/Category.php to app/code/local/Mage/Catalog/Model/Category.php

Step 3: Open app/code/local/Mage/Catalog/Model/Category.php and find text “public function getImageUrl()” and insert below code to create new function for new image attribute to get it’s value with full URL.

Here BannerImage is attribute code:

 public function getBannerImageUrl()
 {
 $url = false;
 if ($image = $this->getBannerimage()) {
 $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
 }
 return $url;
 }

 

Step 4: To call new category image attribute in category view page, please open app/design/frontend/default/YOURTHEMEFOLDER/template/catalog/category/view.phtml

You will get new image attribute with below code: getBannerImageUrl() function name as created above.

 $_category->getBannerImageUrl();

Looking for theme / plugin support?

Looking for theme / plugin customization?