Magento Add Custom Field to Customer Address

Adding Field to Admin

First we need to create the address attribute. There are two ways to do this one is through your module sql file or you can do it through directly through sql query.

First we will see it through the module.

In your module’s config.xml file, add this in the resource tag.


    
      
        Bysoft_Myaddress_Helper
      
    
	
	  
		
		  Bysoft_Myaddress
		  Mage_Customer_Model_Entity_Setup 
		
		
		  core_setup
		
	  
	  
		
		  core_write
		
	  
	  
		
		  core_read
		
	  
	

The important thing is to have your module’s setup class pointed to Mage_Customer_Model_Entity_Setup. Next in the module’s mysql-install file we will put in the code

startSetup();

$this->addAttribute('customer_address', 'buyer_mobile', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'Buyer Mobile',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
    ->getAttribute('customer_address', 'buyer_mobile')
    ->setData('used_in_forms', array('customer_register_address','customer_address_edit','adminhtml_customer_address'))
    ->save();

$installer->endSetup();

 This will add the relevant database entries. If you want to do it directly i.e through database or phpmyadmin. Then you need to do the below.

eav_attribute table

insert a row in this table with values entity_type_id = 2, attribute_code = govt_id, backend_type = varchar, fontend_input = text, fontend_label = Govt ID No#, is_user_defined = 1, is_required = 1, default = NULL.

eav_entity_attribute table

insert a row in this table with values entity_type_id = 2,attribute_set_id =2, attribute_group_id = 2, attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table)

customer_eav_attribute table

insert a row in this table with values attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table), is_visible = 1 rest all column will take default values

customer_form_attribute table

1. insert row in this table with values form_code = adminhtml_customer_address and attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table). This required for the attribute to show up in the admin

2. insert row in this table with values form_code = customer_address_edit and attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table). This required for the attribute to get saved in edit address form and checkout page

3. insert row in this table with values form_code = customer_register_address and attribute_id = (The attribute_id or the primary key of the row inserted in the eav_attribute table). This is required to save the attribute in the create account page

After doing this step, you should be able to do see your new address attribute in the admin.

Address Format Next we need to change the address display format, this format is used everywhere in magento where ever address is displayed. To change the format, go to System -> Configuration -> Customer Configuration -> Address Template There you will see 5 options, we need to change all. 

Text Add {{depend buyer_mobile}}Buyer Mobile# {{var buyer_mobile}}{{/depend}} where ever you want it. {{depend}} basically checks, if buyer_mobile is not empty.

Text One line Add {{depend buyer_mobile}}Buyer Mobile# {{var buyer_mobile}}{{/depend}} where ever you want it. This format shows up in the checkout page shipping,billing address dropdowns. 

HTML Add {{depend buyer_mobile}}
Buyer Mobile# {{var buyer_mobile}}{{/depend}}. This format is used in many places like Order View, Address Display etc. PDF Add {{depend buyer_mobile}}
Buyer Mobile# {{var buyer_mobile}}{{/depend}}|. This format is used in PDF invoices, shipments etc. Javascript Template Add
Buyer Mobile#{buyer_mobile}. This is used in admin add/edit address area. After saving these in the configuration, the new address format should be visible.

 Create Account Address First to show address in the magento create account form we need to make changes to customer\form\register.phtml (magento 1.6- version) or persistent\customer\form\register.phtml (magento 1.6+ version). If your using a module we will overwrite these files and make changes, if your not using a module you can make these changes directly. To overwrite, in your module’s layout xml file


        
            
        

 Next in the phtml file regsiter.phtml created we will copy the default magento register.phtml contents and add our new field. First in the beginning of the file add this code, which will show all the address fields. 

setShowAddressFields(true);?>

 Next we need to add our new field, place this code where you want to show the new field

  •  After this the new field added should be automatically saved to the database. You can view the new field in the admin and my account area.

    Edit Address Next, in the My Account -> Edit Address form we need to add the the new field as well. For this we need to edit the customer\address\edit.phtml file. In the module’s layout xml file put in this code

    
           
               
           
    

     and in the new edit.phtml file put in the form field where every required

                
  •  Now editing/saving the Govt ID will work.

     

    Checkout Billing/Shipping Step

    Now, we will see what we need to do for adding the fields in shipping and billing steps of checkout page.

    For this need to make changes to the checkout/onepage/billing.phtml and checkout/onepage/shipping.phtml files. To do this in the module’s layout xml file put in this code

    
            
                
            
            
                
            
    
    

     Next in the shipping.phtml and billing.phtml files need to add the field. Add this code in billing.phtml file

  •  and add this code in shipping.phtml file

  •  Next, we need to add this xml code to our module config.xml file 

        
            
                
                    *
                    *
                
            
            
                
                    *
                
            
        
      

     Next we need to add govt_id column in the tables sales_flat_order_address, sales_flat_quote_address. To do this in your module’s sql file

    $tablequote = $this->getTable('sales/quote_address');
    $installer->run("
    ALTER TABLE  $tablequote ADD  `buyer_mobile` varchar(255) NOT NULL
    ");
     
    $tablequote = $this->getTable('sales/order_address');
    $installer->run("
    ALTER TABLE  $tablequote ADD  `buyer_mobile` varchar(255) NOT NULL
    ");
    

     Now, if a new address is created from the checkout step the new field will be added to the database and show up the order view.

    These are all the places where the address shows up. The new field we have added will automatically show up in emails, pdfs etc.

    你可能感兴趣的:(php,Magento)