Create new product attribute with options

Hello, Good to see you again. Today i am going to discribe how to create new attribute for product with options and assign to all products with default value. Here i have describe the code to create new attribute and assigned to products.

 

$installer = Mage::getResourceModel('catalog/setup','default_setup');
$installer->startSetup();

Get setup resource model and start installer set-up.

$entity = 'catalog_product';
$newAttribute = 'some_new_attribute';

Define entity type and new attribute code.

$entityTypeId = $installer->getEntityTypeId($entity);

Get entity type id from entity.

$installer->addAttribute($entity, $newAttribute, array(
 'group' => 'general',
 'type' => 'varchar',
 'backend' => '',
 'frontend' => '',
 'label' => 'Some New Attribute Label',
 'input' => 'select',
 'class' => '',
 'source' => 'eav/entity_attribute_source_table',
 'visible' => 1,
 'required' => 0,
 'user_defined' => 1,
 'searchable' => TRUE,
 'filterable' => TRUE,
 'comparable' => FALSE,
 'visible_on_front' => TRUE,
 'unique' => FALSE,
 'apply_to' => '',
 'is_configurable' => '',
 'used_for_promo_rules' => 1,
 'attribute_explanation' => 'Some description for attribute',
 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
 'option' =>
 array(
 'values' =>
 array(
 'some value 1',
 'some value 2', 
 'some value 3', 
 'some value 4', 
 ),
 ),
));

Here have define a code to add new attribute with it’s options and other values.

$attributeOption = Mage::getModel('eav/config')->getAttribute($entity, $newAttribute);

Have get new inserted attribute options from eav/config model.

$defaultAttributeValue = 'some value 2';

Set some default value which want to set as default for all products.

if($attributeOption->getSource()){
  $options = $attributeOption->getSource()->getAllOptions(false);
  foreach($options as $option){
    if($option['label'] == $defaultAttributeValue){
       $defaultValue = $option['value'];
    } 
  }
}

Get default values attribute id to set on products.

 

$installer->addAttribute($entity, $newAttribute, array(
 'group' => 'general',
 'type' => 'varchar',
 'backend' => '',
 'frontend' => '',
 'label' => 'Product Payment Method',
 'input' => 'select',
 'class' => '',
 'source' => 'eav/entity_attribute_source_table',
 'visible' => 1,
 'required' => 0,
 'user_defined' => 1,
 'default' =>$defaultValue,
 'searchable' => TRUE,
 'filterable' => TRUE,
 'comparable' => FALSE,
 'visible_on_front' => TRUE,
 'unique' => FALSE,
 'apply_to' => '',
 'is_configurable' => '',
 'used_for_promo_rules' => 1,
 'attribute_explanation' => 'Payment method can define per product',
 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$installer->endSetup();

Have updated attribute with default value and ended installer script.

$productIdsCollection = Mage::getResourceModel('catalog/product_collection')->getAllIds();

Get all product ids from catalog products resource model.

$storeId = 0;
Mage::getModel('catalog/product_action')->updateAttributes(
 $productIdsCollection,
 array($newAttribute => $defaultValue),
 $storeId
);

This will update attribute for all the products with default value.
Now if you look into any products at admin side you will find a new attribute with it’s default value on dropdown.

Thanks for reading this entire blog.