Monday 11 April 2016

Magento how to add new category attribute

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

<?php
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

//Add selectbox attribure
$setup->addAttribute('catalog_category', 'add_slider', array(
    'group'         => 'Show home on slider',
    'input'         => 'select',
    'type'          => 'varchar',
    'label'         => 'Status',
    'option'        => array ('value' => array('optionone'=>array(0=>'Enable'),'optiontwo'=>array(0=>'Disable')) ),
    'backend'        => 'eav/entity_attribute_backend_array',
    'visible'       => 1,
    'required'      => 0,
    'user_defined' => 1,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

//Add input type text attribure.
$setup->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'slider_title', array(
    'group'         => 'Show home on slider',
    'input'         => 'text',
    'type'          => 'text',
    'label'         => 'Slider title',
    'backend'       => '',
    'visible'       => true,
    'required'      => false,
    'visible_on_front' => true,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));

//Add image attribure
$setup->addAttribute('catalog_category', 'slider_image', array(
    'group'         => 'Show home on slider',
    '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,
)); ?>


To get attributes value paste following code in product list page (list.phtml) and you will get attribute value.

 <?php  $currentcategory = Mage::registry('current_category');
    $catId =  $currentcategory->getId();
    $current_category = Mage::getModel('catalog/category')->load($catId);
    $mediaUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."catalog/category/";
    echo $staus =  $current_category->getAddSlider()."<br/>";
    echo $title =  $current_category->getSliderTitle()."<br/>";
    echo $image =  $mediaUrl.$current_category->getSliderImage()."<br/>";
     ?>

No comments:

Post a Comment