Hello my dearest friends,
Here we are again, meeting up in Magento Tutorials after one week. Before we proceed further, let me ask you a very simple question: “What is the basic difference between a successful and an unsuccessful business?” A business is successful only when its products and services have enough buyers in the market. Yes, there are several other parameters also but customers play a crucial role in deciding the success and failure of an organization. And with such a large number of customer in your system, how can you handle them? Magento helps manage customer easily and conveniently.
1. Customer Entity
Similar to product, Magento uses EAV model to keep customer’s information. The main table to store customer data is customer_entity.
Besides EAV tables, customer’s attributes have an additional table (customer_eav_attribute), you can add validate required to an attribute:
If you want to add attributes in customer’s data, you can use the customer setup class (Mage_Customer_Model_Entity_Setup):
$setup=new Mage_Customer_Model_Entity_Setup(); $setup->addAttribute('customer','national_id',array( 'group'=>'General', 'type'=>'varchar', 'input'=>'text', 'label'=>'National Identification', 'visible'=>1, 'required'=>0, 'visible_on_front'=>1, 'sort_order'=>120 ));
In addition, if you want to show your custom attribute on edit form in backend, you need to add attribute to customer form attribute:
$setup->getConnection()->insertMultiple($setup->getTable('customer/form_attribute'),array( array( 'form_code'=>'adminhtml_customer', 'attribute_id'=>$setup->getAttributeId('customer','national_id') ) ));
2. Magento Customer Address
Magento designs multiple addresses option for one customer. In checkout process, customer address is converted to billing address and shipping address. The billing address is often used to calculate the tax for order, the shipping address is always used to calculate the shipping fee. Magento configures fields that convert from customer address to order address in config.xml file:
...</span></p> ... customer_address_id customer_id * * * * * <!--<email><to_quote_address>*</to_quote_address></email>--> * street * * * * * * *
Please note that EAV Model stores customer address. Therefore, adding attributes in it is similar to customer entity, which entity type is customer_address.
3. Configure validation customer information
To change the validation of customer information, you can go to System > Configuration > Customer Configuration and find the field set Name and Address Options:
When you save the configuration, the customer attribute is saved too. You can find the code to save it on class Mage_Adminhtml_Model_System_Config_Backend_Customer_Show_Customer and method _afterSave():
protectedfunction _afterSave() { $result=parent::_afterSave(); $valueConfig=array( ''=>array('is_required'=>0,'is_visible'=>0), 'opt'=>array('is_required'=>0,'is_visible'=>1), '1'=>array('is_required'=>0,'is_visible'=>1), 'req'=>array('is_required'=>1,'is_visible'=>1), ); $value=$this->getValue(); if(isset($valueConfig[$value])){ $data=$valueConfig[$value]; }else{ $data=$valueConfig['']; } if($this->getScope()=='websites'){ $website= Mage::app()->getWebsite($this->getWebsiteCode()); $dataFieldPrefix='scope_'; }else{ $website=null; $dataFieldPrefix=''; } foreach($this->_getAttributeObjects()as$attributeObject){ if($website){ $attributeObject->setWebsite($website); $attributeObject->load($attributeObject->getId()); } $attributeObject->setData($dataFieldPrefix.'is_required',$data['is_required']); $attributeObject->setData($dataFieldPrefix.'is_visible',$data['is_visible']); $attributeObject->save(); } return$result; }
Therefore, you can add configuration to your custom attribute with the backend model (tag backend_model) as the following instruction: adminhtml/system_config_backend_customer_show_customer. The configuration will be automatically applied to your custom attribute that you added.
Well, that’s the end of today tutorial. Hope it’ll help you in managing customer data to make the most from your site! Feel free to leave any questions in comment and see you next time in Magestore Magento Blog!