Add custom field in the billing and shipping address in the checkout and customer address edit page in frontend and customer addresses details in Admin.

Last modified by Md Zafar on Fri, May 6, 2011 02:16
Source|Old Revisions  

This is an old revision of the document!



In my case I needed to add Job Title field above the Company field in billing and shipping address.

First go to Magento installation directory and edit these three files to add Job Title text box in frontend.

1.app/design/frontend/default/your_theme/template/customer/address/edit.phtml.

              <li class="fields">
              <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getAddress()->getFirstname() ? $this->getAddress() : $this->getCustomer())->toHtml() ?>
          </li>
          <li class="wide">
              <label for="jobtitle"><?php echo $this->__('Job Title') ?></label>
              <div class="input-box">
                  <input type="text" name="jobtitle" id="jobtitle" title="<?php echo $this->__('Job Title') ?>" value="<?php echo $this->htmlEscape($this->getAddress()->getJobtitle()) ?>" class="input-text" />
              </div>
          </li>
          <li class="wide">
              <label for="company"><?php echo $this->__('Company') ?></label>
              <div class="input-box">
                  <input type="text" name="company" id="company" title="<?php echo $this->__('Company') ?>" value="<?php echo $this->htmlEscape($this->getAddress()->getCompany()) ?>" class="input-text" />
              </div>
          </li>

2.app/design/frontend/default/your_theme/template/checkout/onepage/billing.phtml.

                      <div class="field">
                      <label for="billing:jobtitle"><?php echo $this->__('Job Title') ?></label>
                      <div class="input-box">
                          <input type="text" id="billing:jobtitle" name="billing[jobtitle]" value="<?php echo $this->htmlEscape($this->getAddress()->getJobtitle()) ?>" title="<?php echo $this->__('Job Title') ?>" class="input-text" />
                      </div>
                  </div>
                  <div class="field">
                      <label for="billing:company"><?php echo $this->__('Company') ?></label>
                      <div class="input-box">
                          <input type="text" id="billing:company" name="billing[company]" value="<?php echo $this->htmlEscape($this->getAddress()->getCompany()) ?>" title="<?php echo $this->__('Company') ?>" class="input-text" />
                      </div>
                  </div>

3.app/design/frontend/default/your_theme/template/checkout/onepage/shipping.phtml.

                          <div class="fields">
                          <label for="shipping:jobtitle"><?php echo $this->__('Job Title') ?></label>
                          <div class="input-box">
                              <input type="text" id="shipping:jobtitle" name="shipping[jobtitle]" value="<?php echo $this->htmlEscape($this->getAddress()->getJobtitle()) ?>" title="<?php echo $this->__('Job Title') ?>" class="input-text" onchange="shipping.setSameAsBilling(false);" />
                          </div>
                      </div>
                      <div class="fields">
                          <label for="shipping:company"><?php echo $this->__('Company') ?></label>
                          <div class="input-box">
                              <input type="text" id="shipping:company" name="shipping[company]" value="<?php echo $this->htmlEscape($this->getAddress()->getCompany()) ?>" title="<?php echo $this->__('Company') ?>" class="input-text" onchange="shipping.setSameAsBilling(false);" />
                          </div>
                      </div>

Next I created a separate module. app/code/local/Tdg/Check and saved the configuration file under app/etc/modules/Tdg_All.xml

I created these files under app/code/local/Tdg/Check directory:-

1.app/etc/modules/Tdg_All.xml.

      <?xml version="1.0" encoding="UTF-8"?>
      <config>
  <modules>
      <Tdg_Check>
          <active>true</active>
          <codePool>local</codePool>
      </Tdg_Check>
  </modules>
      </config>

2.app/code/local/Tdg/Check/etc/config.xml.

  <?xml version="1.0" encoding="UTF-8"?>
  <config>
  <modules>
      <Tdg_Check>
          <version>1.0.0</version>
      </Tdg_Check>
  </modules>
  <admin>
      <fieldsets>
          <customer_dataflow>
              <jobtitle><billing>1</billing><shipping>1</shipping></jobtitle>
          </customer_dataflow>
      </fieldsets>
  </admin>
  <global>
      <models>
          <check>
              <class>Tdg_Check_Model</class>
          </check>
      </models>
      <resources>
          <check_setup>
              <setup>
                  <module>Tdg_Check</module>
              </setup>
              <connection>
                  <use>core_setup</use>
              </connection>
          </check_setup>
          <check_write>
              <connection>
                  <use>core_write</use>
              </connection>
          </check_write>
          <check_read>
              <connection>
                  <use>core_read</use>
              </connection>
          </check_read>
      </resources>
      <fieldsets>
          <sales_copy_order_billing_address>
              <jobtitle><to_order>*</to_order></jobtitle>
          </sales_copy_order_billing_address>
          <sales_copy_order_shipping_address>
              <jobtitle><to_order>*</to_order></jobtitle>
          </sales_copy_order_shipping_address>
          <sales_convert_quote_address>
              <jobtitle><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></jobtitle>
          </sales_convert_quote_address>
          <sales_convert_order_address>
              <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
          </sales_convert_order_address>
          <customer_address>
               <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
          </customer_address>
          <checkout_onepage_billing>
              <jobtitle><to_customer>*</to_customer></jobtitle>
          </checkout_onepage_billing>
      </fieldsets>
  </global>
  </config>

The below tag in the above config.xml I copied from the app/code/core/Mage/Customer/etc/config.xml and added the line <jobtitle><billing>1</billing><shipping>1</shipping></jobtitle>

      <admin>
      <fieldsets>
          <customer_dataflow>
              <jobtitle><billing>1</billing><shipping>1</shipping></jobtitle>
          </customer_dataflow>
      </fieldsets>
      </admin>

The below tag under the <global> in the above config.xml I copied from the app/code/core/Mage/Sales/etc/config.xml and made below changes.Under the <sales_copy_order_billing_address> tag added <jobtitle><to_order>*</to_order></jobtitle>.Under the <sales_copy_order_shipping_address> tag added <jobtitle><to_order>*</to_order></jobtitle>.Under the <sales_convert_quote_address> tag added the <jobtitle><to_order_address>*</to_order_address> <to_customer_address>*</to_customer_address></jobtitle> .Under the <sales_convert_order_address> tag added <jobtitle><to_quote_address>*</to_quote_address></jobtitle>.Under the <customer_address> tag I added <jobtitle><to_quote_address>*</to_quote_address></jobtitle>.

           <sales_copy_order_billing_address>
              <jobtitle><to_order>*</to_order></jobtitle>
          </sales_copy_order_billing_address>
          <sales_copy_order_shipping_address>
              <jobtitle><to_order>*</to_order></jobtitle>
          </sales_copy_order_shipping_address>
          <sales_convert_quote_address>
              <jobtitle><to_order_address>*</to_order_address>     <to_customer_address>*</to_customer_address></jobtitle>
          </sales_convert_quote_address>
          <sales_convert_order_address>
              <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
          </sales_convert_order_address>
          <customer_address>
               <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
          </customer_address>

The below tag under the <global> tag in the file app/code/local/Tdg/Check/etc/config.xml I copied from app/code/core/Mage/Checkout/etc/config.xml and made some changes.Under the <checkout_onepage_billing> added <jobtitle><to_customer>*</to_customer></jobtitle>.

          <checkout_onepage_billing>
              <jobtitle><to_customer>*</to_customer></jobtitle>
          </checkout_onepage_billing>

Next I created setup file to create “jobtitle” attribute under customer_address entity while installing the module.

2.app/code/local/Tdg/Checl/sql/check_setup/mysql4-install-1.0.0.php file.

/* @var $installer Mage_Customer_Model_Entity_Setup */
$installer = $this;
$installer->startSetup();
/* @var $addressHelper Mage_Customer_Helper_Address */
$addressHelper = Mage::helper('customer/address');
$store         = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);

/* @var $eavConfig Mage_Eav_Model_Config */
$eavConfig = Mage::getSingleton('eav/config');

// update customer address user defined attributes data
$attributes = array(
    'jobtitle'           => array(    
        'label'    => 'Job Title',
        'type'     => 'varchar',
        'input'    => 'text',
        'is_user_defined'   => 1,
        'is_system'         => 0,
        'is_visible'        => 1,
        'sort_order'        => 140,
        'is_required'       => 1,
        'multiline_count'   => 0,
        'validate_rules'    => array(
            'max_text_length'   => 255,
            'min_text_length'   => 1
        ),
    ),
);

foreach ($attributes as $attributeCode => $data) {
    $attribute = $eavConfig->getAttribute('customer_address', $attributeCode);
    $attribute->setWebsite($store->getWebsite());
    $attribute->addData($data);
        $usedInForms = array(
            'adminhtml_customer_address',
            'customer_address_edit',
            'customer_register_address'
        );
        $attribute->setData('used_in_forms', $usedInForms);
    $attribute->save();
}

$installer->run("
    ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `jobtitle` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
     ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `jobtitle` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
    ");
$installer->endSetup();

The above code will add “jobtitle” attribute in eav_attribute table in database of entity type customer_address. Next it will add a row in customer_eav_attribute table. Next it will add three rows in customer_form_attribute table with form codes adminhtml_customer_address,customer_address_edit & customer_register_address with attribute id of the jobtitle attribute. Next it will add one column “jobtitle” in sales_flat_quote_address & sales_order_quote_address table.

Next I made some changes to the address templates in Magento Admin. Go to System→Configurations→Customers→Customer Configurations→Address Templates

Add this code in the “Text” textarea:-

{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}
{{depend jobtitle}}{{var jobtitle}}{{/depend}}
{{depend company}}{{var company}}{{/depend}}
{{if street1}}{{var street1}}
{{/if}}
{{depend street2}}{{var street2}}{{/depend}}
{{depend street3}}{{var street3}}{{/depend}}
{{depend street4}}{{var street4}}{{/depend}}
{{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
{{var country}}
T: {{var telephone}}
{{depend fax}}F: {{var fax}}{{/depend}}

Add this code in the “Html” textarea

{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}<br/>
{{depend jobtitle}}{{var jobtitle}}<br />{{/depend}}
{{depend company}}{{var company}}<br />{{/depend}}
{{if street1}}{{var street1}}<br />{{/if}}
{{depend street2}}{{var street2}}<br />{{/depend}}
{{depend street3}}{{var street3}}<br />{{/depend}}
{{depend street4}}{{var street4}}<br />{{/depend}}
{{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}<br/>
{{var country}}<br/>
{{depend telephone}}T: {{var telephone}}{{/depend}}
{{depend fax}}<br/>F: {{var fax}}{{/depend}}

Add the below code in the “PDF” text area.

{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}|
{{depend jobtitle}}{{var jobtitle}}|{{/depend}}
{{depend company}}{{var company}}|{{/depend}}
{{if street1}}{{var street1}}
{{/if}}
{{depend street2}}{{var street2}}|{{/depend}}
{{depend street3}}{{var street3}}|{{/depend}}
{{depend street4}}{{var street4}}|{{/depend}}
{{if city}}{{var city}},  {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}|
{{var country}}|
{{depend telephone}}T: {{var telephone}}{{/depend}}|
{{depend fax}}<br/>F: {{var fax}}{{/depend}}|

Save the configuration.After installing the module refresh cache once from Admin.

Checkout the screen shots below:-

{{checkout_shipping.jpg|{{customer_add_edit_address.jpg|{{customer_address_book.jpg|{{customer_address_admin.jpg|{{customer_address_order.jpg|}}}}}}}}}}




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs