Hello guys, let’s come to another interesting tutorial in Magento blog!

Recently some people have told me that sometimes they want to add new product attributes which Magento core has not supported but they don’t know exactly how to use the code to do that. Therefore, I have written this Magento tutorial and hope that it’s helpful for whom are encountering this issue. To create a new product attribute in Magento, you can use the piece of PHP code below:

//get entity_type_id
$entity_type = Mage::getSingleton("eav/entity_type")->loadByCode("catalog_product");
$entity_type_id = $entity_type->getId();

//check the exist of the product attribute
$collection = Mage::getModel("eav/entity_attribute")
->getCollection()
->addFieldToFilter("entity_type_id",$entity_type_id)
->addFieldToFilter("attribute_code","new_product_attribute");

if(!count($collection))
{

$installer = Mage::getModel(‘core/resource_setup’);
$attribute = $collection->getFirstItem();
$data = array();
$data['id'] = null;
$data['entity_type_id'] = $entity_type_id;
$data['attribute_code'] = "new_product_attribute";
/*This is the name of the attribute used by the system. Spaces can not be used in this field. We’ll type
new_product_attribute.*/

$data['frontend_input'] = "boolean";
/*This describes what kind of data the attribute will store. What’s set here determines how data entry for this attribute will take place. We’ll use Yes/No so we enter “boolean”
Eg:
• text: Text Field
• textarea: Text Area
• date: Date
• boolean: Yes/No
• multiselect: Multiple Select
• select: Dropdown
• price: Price
• media_image: Media Image
• weee: Fixed Product Tax
*/
$data['backend_type'] = $attribute->getBackendTypeByInput($data['frontend_input']);
$data['frontend_label'] = 'New Product Attribute';
$attribute->setData($data);
$attribute->save();

$resource = Mage::getSingleton('core/resource');
$read= $resource->getConnection('core_read');
$write = $resource->getConnection('core_write');

//get attribute_set_id
$select = $read->select()
->from($installer ->getTable("eav_attribute_set"),array('attribute_set_id'))
->where("entity_type_id=?",$entity_type_id);
$attribute_sets = $read->fetchAll($select);
foreach($attribute_sets as $attribute_set) {
$attribute_set_id = $attribute_set['attribute_set_id'];
$select = $read->select()
->from($installer ->getTable("eav_attribute"),array('attribute_id'))
->where("entity_type_id=?",$entity_type_id)
->where("attribute_code=?","new_product_attribute");
$attribute = $read->fetchRow($select);
$attribute_id = $attribute['attribute_id'];
$select = $read->select()
->from($installer->getTable("eav_attribute_group"),array('attribute_group_id'))
->where("attribute_set_id=?",$attribute_set_id)
->where("attribute_group_name=?","General");
$attribute_group = $read->fetchRow($select);
$attribute_group_id = $attribute_group['attribute_group_id'];
$write->beginTransaction();
$write->insert($installer->getTable("eav_entity_attribute"),array("entity_type_id"=>$entity_type_id,"attribute_set_id"=>$attribute_set_id,"attribute_group_id"=>$attribute_group_id,"attribute_id"=>$attribute_id,"sort_order"=>0));
$write->commit();
}
}

Have your new product attribute has been successfully created yet? If you need tutorials for other Magento framework, just visit our Magento blog!

Related Tutorials:

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

Write A Comment