`
kirenenko04
  • 浏览: 146361 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

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.

<global>
    <helpers>
      <myaddress>
        <class>Bysoft_Myaddress_Helper</class>
      </myaddress>
    </helpers>
	<resources>
	  <myaddress_setup>
		<setup>
		  <module>Bysoft_Myaddress</module>
		  <class>Mage_Customer_Model_Entity_Setup</class> <!-- This is the important thing-->
		</setup>
		<connection>
		  <use>core_setup</use>
		</connection>
	  </myaddress_setup>
	  <myaddress_write>
		<connection>
		  <use>core_write</use>
		</connection>
	  </myaddress_write>
	  <myaddress_read>
		<connection>
		  <use>core_read</use>
		</connection>
	  </myaddress_read>
	</resources>

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

<?php
$installer = $this;
$installer->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}}<br/>Buyer Mobile# {{var buyer_mobile}}{{/depend}}. This format is used in many places like Order View, Address Display etc. 
PDF Add {{depend buyer_mobile}}<br/>Buyer Mobile# {{var buyer_mobile}}{{/depend}}|. This format is used in PDF invoices, shipments etc. 

Javascript Template Add <br/>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

<customer_account_create>
        <reference name='customer_form_register'>
            <action method='setTemplate'><template>address/persistent/customer/form/register.phtml</template></action>
        </reference>
</customer_account_create>

 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. 

<?php $this->setShowAddressFields(true);?>

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

<li class="fields">
    <div class="field">
        <label for="buyer_mobile" class="required"><em>*</em><?php echo $this->__('Buyer Mobile') ?></label>
         <div class="input-box">
                 <input type="text" name="buyer_mobile" value="<?php echo $this->htmlEscape($this->getFormData()->getData('buyer_mobile')) ?>" title="<?php echo $this->__('Buyer Mobile') ?>" id="buyer_mobile" class="input-text required-entry" />
          </div>
      </div>
</li>

 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

<customer_address_form>
       <reference name='customer_address_edit'>
           <action method='setTemplate'><template>address/customer/address/edit.phtml</template></action>
       </reference>
</customer_address_form>

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

            <li class="fields">
                 <div class="field">
                        <label for="buyer_mobile" class="required"><em>*</em><?php echo $this->__('Buyer Mobile') ?></label>
                        <div class="input-box">
                            <input type="text" name="buyer_mobile" value="<?php echo $this->htmlEscape($this->getAddress()->getData('buyer_mobile')) ?>" title="<?php echo $this->__('Buyer Mobile') ?>" id="buyer_mobile" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone'); ?> required-entry" />
                        </div>
                    </div>
           </li>

 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

<checkout_onepage_index>
        <reference name='checkout.onepage.billing'>
            <action method='setTemplate'><template>address/checkout/onepage/billing.phtml</template></action>
        </reference>
        <reference name='checkout.onepage.shipping'>
            <action method='setTemplate'><template>address/checkout/onepage/shipping.phtml</template></action>
        </reference>
</checkout_onepage_index>

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

<li class="buyer_mobile">
    <div class="buyer_mobile_field">
         <div class="left_label"><label for="billing:buyer_mobile" class="required"><em>*</em><?php echo $this->__('Buyer Mobile') ?></label></div>
         <div class="buyer_mobile_content">
               <input type="text" title="<?php echo $this->__('Buyer Mobile') ?>" name="billing[buyer_mobile]" id="billing:buyer_mobile" value="<?php echo $this->htmlEscape($this->getAddress()->getBuyerMobile()) ?>" class="input-text validate-one-required-by-group required-group-1 validate-digits  validate-length minimum-length-11 maximum-length-11" />
          </div>
     </div>
</li>

 and add this code in shipping.phtml file

<li class="buyer_mobile">
    <div class="buyer_mobile_field">
         <div class="left_label"><label for="billing:buyer_mobile" class="required"><em>*</em><?php echo $this->__('Buyer Mobile') ?></label></div>
         <div class="buyer_mobile_content">
               <input type="text" title="<?php echo $this->__('Buyer Mobile') ?>" name="billing[buyer_mobile]" id="billing:buyer_mobile" value="<?php echo $this->htmlEscape($this->getAddress()->getBuyerMobile()) ?>" class="input-text validate-one-required-by-group required-group-1 validate-digits  validate-length minimum-length-11 maximum-length-11" />
          </div>
     </div>
</li>

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

    <fieldsets>
        <sales_convert_quote_address>
            <buyer_mobile>
                <to_order_address>*</to_order_address>
                <to_customer_address>*</to_customer_address>
            </buyer_mobile>
        </sales_convert_quote_address>
        <customer_address>
            <buyer_mobile>
                <to_quote_address>*</to_quote_address>
            </buyer_mobile>
        </customer_address>
    </fieldsets>
  </global>

 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.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics