Tips to create magento custom attribute

custom attribute magento

In Magento an attribute means any property of an entity ( Product, Customer ) and we can add as many attributes as we need. They can be grouped into attribute sets. An attribute set is a collection of similar attributes. An attribute is very important to show extra information like Color , Size, Brand etc of Product or Customer on front end. Recently we created an E-auction system for one of our client. Basically this auction system system contains three entity Grower, Buyer and Back-office. We have also created Gemtree Grower and Gemtree Buyer application. They want to show some extra information of Product or Customer like Bidding Type, Bidder Name, Bidder address, Current Bid Price etc on front end or application. So need to create custom attribute for product. In this article we have demonstrated ways to create custom attribute in magento . Basically there are two way to create custom product attributes.

1.Create magento custom attribute from Back-end
2. Create magento custom attribute Pro-grammatically .

Create magento custom attribute from Back end

Follow the below steps to create custom products attribute from back end.

1 . Log into the Magento admin panel. Go to Catalog -> Attributes -> Manage Attributes
2 . Click on “Add New Attribute” on top right.
3 . Click on the Properties tab and configure the attribute properties and front end properties as needed.

create-attribute

4 . Click Manage Label/Options tab and enter the label name and label options.

create-label-options
5 . Click on save attribute.

Create magento custom attribute Pro-grammatically

We can create product attribute pro-grammatically . Below is the code for create product attribute.

$model=Mage::getModel('eav/entity_setup','core_setup');
$attribute_code = 'product_owner';
$model->->addAttribute('catalog_product', $attribute_code , array(
  'group' => 'simple', 
  'type'       => 'varchar',
  'input'      => 'select',
  'label'      => 'Product Owner',
  'visible' => 1,  
  'sort_order' => 10,
  'visible' => 1,
  'required' => 0,
  'user_defined' => 1,
  'searchable' => 0,
  'filterable' => 0,
  'comparable' => 0,
  'visible_on_front' => 0,
  'visible_in_advanced_search' => 0,
  'is_html_allowed_on_front' => 0,
  'is_configurable' => 1,
  'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
  'backend'    => 'eav/entity_attribute_backend_array',
  'option'     => array (
        'values' => array(
            0 => 'No',
            1 => 'Yes',
        )
    ),

));

Below is code for create attribute options dynamically.

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$attribute_code = 'product_owner'; //this should be already exist
//This would be dynamic value
$owners = array('Alina','Janifer','Smith');
$owners = array_map('utf8_encode',$owner); //If you're using special characters
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$options = array();
$options['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, $attribute_code);
for($i=0;$i<sizeof($owners);$i++){
   $options['value']['option'.$i][0] = $owners[$i];
}
$installer->addAttributeOption($options);
$installer->endSetup();

Conclusion

Hence, using this we can create custom attributes and attribute options dynamically for product and customer. Feel free to contact us if you have any further query.

References

  1. http://inchoo.net/magento/programatically-create-attribute-in-magento-useful-for-the-on-the-fly-import-system/
Tips to create magento custom attribute was last modified: March 8th, 2016 by Ajay Mehta