Adding Custom Attributes in Magento EAV
As a senior developer at Laravel Company, I often encounter clients who need to add custom attributes to their Magento stores while using the EAV (Entity Attribute Value) model. In this blog post, I will guide you through the process of adding new customer attributes and making them editable from both the frontend and backend.
Creating New Customer Attributes
To create new customer attributes in your EAV module, you need to follow these steps:
- Create a new file named
install.phpinside your custom module’setcdirectory. This file will contain the installation script for your custom attributes. - In the
install.phpfile, define an array of attributes with their corresponding configurations. Here is an example of adding two custom attributes: profile image and mobile number.
<?php
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'profile_image', array(
'label' => 'Profile Image',
'type' => 'varchar',
'input' => 'file',
'visible' => true,
'required' => false,
'user_defined' => true,
));
$setup->addAttribute('customer', 'mobile', array(
'label' => 'Mobile Number',
'type' => 'int',
'input' => 'text',
'visible' => true,
'required' => false,
'user_defined' => true,
));
$installer->endSetup();
- In the
etcdirectory, create another file namedconfig.xml. This file will define the configuration settings for your custom attributes. - Inside the
config.xmlfile, define the entity type, attribute set, and other necessary configurations. Here is an example:
<config>
<modules>
<Namespace_Customer>
<version>0.1.0</version>
</Namespace_Customer>
</modules>
<global>
<models>
<customer>
<class>Namespace_Customer_Model</class>
<resourceModel>Namespace_Customer_Model_Resource</resourceModel>
</customer>
</models>
</global>
</config>
Adding Custom Attributes to the Customer Form
To add your custom attributes to the customer edit form in the frontend, follow these steps:
- Create a new file named
Form.phpinside your custom module’sBlock/Adminhtml/directory. - In the
Form.phpfile, extend theMage_Adminhtml_Block_Templateclass and override the_prepareForm()method to add your custom attributes.
<?php
class Namespace_Customer_Block_Adminhtml_Form extends Mage_Adminhtml_Block_Template
{
public function _prepareForm()
{
$form = new Varien_Data_Form();
// Add your custom attributes here using the $form->addField($fieldName, $type) method
return $form;
}
}
- In your
config.xmlfile, map the custom form block to the appropriate controller and action.
<admin>
<routers>
<customer>
<args>
<modules>
<Namespace_Customer before="Mage_Adminhtml">
<class>Namespace_Customer_Block_Adminhtml</class>
</Namespace_Customer>
</modules>
</args>
</customer>
</routers>
</admin>
Preparing the Grid for Custom Attributes
To display your custom attributes in the customer grid, you need to modify the Grid.php file of your custom module’s Block/Adminhtml/ directory.
- Override the
_prepareCollection()method in yourGrid.phpfile and retrieve the collection using the appropriate model.
protected function _prepareCollection()
{
$collection = Mage::getModel('namespace_customer/eavcustomer')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
- Create a new
Grid.phpfile inside your custom module’sBlock/Adminhtml/directory and extend theMage_Adminhtml_Block_Widget_Gridclass. - In the new
Grid.phpfile, override the_prepareColumns()method to include columns for your custom attributes.
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('namespace_customer')->__('ID'),
'index' => 'entity_id',
));
// Add additional columns for your custom attributes here
return parent::_prepareColumns();
}
Conclusion
Adding custom attributes to the Magento EAV customer module can be a complex task, but with proper understanding and following these steps, you can successfully create new attributes and make them editable from both the frontend and backend. Make sure to update your codebase accordingly to ensure compatibility with your existing modules.
If you need further assistance or want to explore more advanced customizations in Magento, feel free to visit Laravel Company for expert services and solutions tailored to your specific requirements.