Monday, November 9, 2015

Add a Custom Attribute to the Category in Magento


Magento use for creating an e - commerce website so in that category is the most important functionality. When you install new Magento than there is one category named “Default Category” assign and all new categories and subcategories. We select a product category, so that a product display in associate category in Magento. So all products display in the website according to category and all category display in the Magento main menu.
In Magento category, there are some default fields provide by Magento like Name, Description, Image, Thumbnail Image, Meta Keywords and Description. When you fill the details that its display on front category page according to a filed location.
Sometimes you need an extra category filed like extra category images, link page or any date or text field so you can do with simple creating PHP file in a Magento root folder or anywhere with call “Mage.php” file for use Magento functions. Here below code for adding extra category image field with instruction for another field

require_once "app/Mage.php";
if (!Mage::isInstalled())
{
 echo "Application is not installed yet, please complete install wizard first.";
 exit;
}
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$installer = new Mage_Eav_Model_Entity_Setup('core_setup'); 
$installer->startSetup();
$attribute  = array(
 'type' => 'varchar', //datetime,decimal,int,static,text,varchar
 'label'=> 'Category Image 1', //custome field label
 'input' => 'image', //date,image,media_image,gallery,multiselect,price,select,textarea,text,weight
  /*
   for date select type='datetime'
   for image/media_image/gallery select type='varchar'
   for price/weight select type='decimal'
  */
 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
 'default' => '1', //set default value
 'source'  => 'catalog/category_attribute_source_sortby', //select model for select and multiselect dropdown values
 /*
 for date 'eav/entity_attribute_backend_datetime'
 for YES/NO selection 'eav/entity_attribute_source_boolean'
 for page layout selection 'catalog/category_attribute_source_layout'
 for ENABLE/DISABLE selection 'catalog/product_status' 
 */
 'backend' => 'catalog/category_attribute_backend_image', //select backend model for save data
 /*
 for image/media_image 'catalog/category_attribute_backend_image'
 for date 'eav/entity_attribute_backend_datetime'
 for price 'catalog/product_attribute_backend_price'
 for gallery 'catalog/product_attribute_backend_media'
 */
 'required' => false, //is required or not
 'sort_order' => 1, //set order
 'group' => "General Information" //General Information,Display Settings,Custom Design
);
$installer->addAttribute('catalog_category', 'category_image_1', $attribute);
$installer->endSetup();

When you run below code file than new category fields added to your Magento admin panel. You can see on catalog->categories and edit any category. If you didn’t see them clear all caches run compile and then check again.
Now it’s also important for display or getting extra field value in front. For this, just use below code according to category id and extra field name assign while creating new in below code.

$category_id = '3'; //enter category id for get custome field data
$cat = Mage::getModel("catalog/category")->load($category_id);
echo $cat->getData('category_image_1');
//for image get full path
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/category/'.$cat->getData('category_image_1');

No comments :

Post a Comment