Custom Account/Registration Fields

Last modified by Algiasa on Fri, June 25, 2010 09:50
Source|Old Revisions  

This is an old revision of the document!


[Update 23 Dec 2008 - phlux0r] Be aware that editing Magento Core files is not recommended as your changes may be overwritten by future updates. [/Update 23 Dec 2008 - phlux0r]

The better approach is to override the functionality in your local namespace as this will protect your changes from updates. I have successfully added a new Mobile Phone attribute to the address records on the Registration, One page checkout (for guest and registered) and multishipping pages. An overview of what I’ve done can be found at the end of this article.

Bare in mind that my changes relate to adding a mobile phone field to the addresses NOT the Occupation field to the customer data.

[Update 3 Oct 2008 - Fibo]Completed on how to display the info in the Edit Form –Note: done on the old 1.1.3, your mileage may vary[/Update 3 Oct 2008 - Fibo]

[Update 30 Aug 2008 - AlexSz]Info on how to make this work at the end of this post.[/Update 30 Aug 2008 - AlexSz]

Alright, we will be dealing with 5 files. So open each of the following:

app/design/frontend/default/yourtheme/template/customer/form/register.phtml - Has the HTML form for initial registration Note: this file will also be used for a one-shot database extension

app/design/frontend/default/yourtheme/template/customer/form/edit.phtml - Has the HTML form for edit by customers

app/code/core/Mage/Customer/Model/Entity/Setup.php - Has an array full of customer attributes to use for registration

app/code/core/Mage/Customer/controllers/AccountController.php - Has a specific block of code for registration - this is no more needed

app/code/core/Mage/Customer/etc/config.xml - Has a description of fields to be used in the forms

Alright, for this little tutorial, we will do a couple of fields. The first one will be a text box asking the customer for their occupation/title. Navigate to somewhere around line 54 of register.html - You should see this block of code:

  1. <li>
  2.     <div class="input-box">
  3.         <label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
  4.         <input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="validate-email required-entry input-text" />
  5.     </div>
  6. </li>

Now, we need to add our code for the occupation text box, so change that block of code to the following:

  1. <li>
  2.     <div class="input-box">
  3.         <label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
  4.         <input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="validate-email required-entry input-text" />
  5.     </div>
  6.     <div class="input-box">
  7.         <label for="occupation"><?php echo $this->__('Occupation/Title') ?></label><br/>
  8.         <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getFormData()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
  9.     </div>
  10. </li>

Ok, if you refresh your create account (register) page, you will see the new field show up.

Do a similar thing in edit.phtml, replacing getFormData by getCustomer. In my 1.1.3 version, this is at line 32. Old code:

  1.         <li>
  2.             <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
  3.         </li>
  4.         <li>
  5.             <div class="input-box">
  6.             <label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
  7.             <input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="required-entry validate-email input-text" />
  8.             </div>
  9.         </li>

New code:

  1.         <li>
  2.             <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
  3.         </li>
  4.         <li>
  5.             <div class="input-box">
  6.             <label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
  7.             <input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="required-entry validate-email input-text" />
  8.             </div>
  9.         </li>
  10.             <li>
  11.                 <div class="input-box">
  12.                     <label for="occupation"><?php echo $this->__('Occupation') ?> </label><br/>
  13.                     <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getCustomer()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
  14.                 </div>
  15.             </li>

Now, the fun part is getting that value into the database. Now, navigate to around line 93 in Setup.php - you should see this block of code:

  1. 'email' => array(
  2.                         'type'          => 'static',
  3.                         'label'         => 'Email',
  4.                         'class'         => 'validate-email',
  5.                         'sort_order'    => 6,
  6.                     ),

Now, we need to add our attribute to this file. So, change this block of code to:

  1. 'email' => array(
  2.                         'type'          => 'static',
  3.                         'label'         => 'Email',
  4.                         'class'         => 'validate-email',
  5.                         'sort_order'    => 6,
  6.                     ),
  7. 'occupation' => array(
  8.                         'label'         => 'Occupation',
  9.                         'required'      => false,
  10.                         'sort_order'    => 7,
  11.                     ),

Right under your occupation array, you will see the group_id one, etc. You need to increment the sort_order as they were doing before. So, since the sort_order for occupation is 7, make the one after it 8, and so on.


This part is needed no more, left here for possible reference - should be deleted at the end of October 2008

Now, head to line 164 of AccountController.php. You should see this block of code:

  1. $customer = Mage::getModel('customer/customer')
  2.     ->setFirstname($this->getRequest()->getPost('firstname'))
  3.     ->setLastname($this->getRequest()->getPost('lastname'))
  4.     ->setEmail($this->getRequest()->getPost('email'))
  5.     ->setPassword($this->getRequest()->getPost('password'))
  6.     ->setConfirmation($this->getRequest()->getPost('confirmation'))
  7.     ->setId(null);

We need to add our new occupation attribute to this, so change the code to this:

  1. $customer = Mage::getModel('customer/customer')
  2.                 ->setFirstname($this->getRequest()->getPost('firstname'))
  3.                 ->setLastname($this->getRequest()->getPost('lastname'))
  4.                 ->setEmail($this->getRequest()->getPost('email'))
  5.                 ->setOccupation($this->getRequest()->getPost('occupation')) //Add this
  6.                 ->setPassword($this->getRequest()->getPost('password'))
  7.                 ->setConfirmation($this->getRequest()->getPost('confirmation'))
  8.                 ->setId(null);
End of the unneeded code

Now, our code is all set up and ready. However, we still need to add this attribute to the eav_attribute table in your mysql database. How do we do that? This block of code:

  1. <?php
  2. $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
  3. $AttrCode = 'occupation';
  4. $settings = array (
  5.     'position' => 1,
  6.     'is_required'=> 0
  7. );
  8. $setup->addAttribute('1', $AttrCode, $settings);
  9. ?>

My suggestion is to add this block of code to the very top of your register.html file. You only need to load this block of code once, so add it to your register.html file, navigate to your register.html file, and then remove the block of code. If you go to your mysql database, view the eav_attribute table and do a search for occupation, you will see that your attribute has been added. Note: when doing all that is described here on my 1.1.3 version, the fields I was including got the “required” flag; I manually edited it in the eav_attribute table – Fibo

Now, go back to your create account page, fill it out, including your occupation, and bam you’re finished. If you need proof that it was added to the database, simply go to the customer_entity_varchar table and look for yourself smile

Also, to retrieve the customer data (as if you were in the account dashboard or something) you call this function: getOccupation.

Written by: Chris Woodard

[Update 30 Aug 2008 - AlexSz] In order to insert the values from custom registration fields you have to edit a .xml file: app/code/core/Mage/Customer/etc/config.xml Inside this file you will find the tag <fieldsets>. To get this to work you have to add the field here. I’ve added the line:

  1. <occupation><create>1</create><update>1</update></occupation>

I’m attaching an excerpt of this file below. Also, you don’t need to edit the AccountController.php as the Mangento team apparently made that part of the code more generic (it reads the fields from the config.xml file).

  1. <fieldsets>
  2.             <customer_account>
  3.                 <prefix><create>1</create><update>1</update><name>1</name></prefix>
  4.                 <firstname><create>1</create><update>1</update><name>1</name></firstname>
  5.                 <middlename><create>1</create><update>1</update><name>1</name></middlename>
  6.                 <lastname><create>1</create><update>1</update><name>1</name></lastname>
  7.                 <suffix><create>1</create><update>1</update><name>1</name></suffix>
  8.                 <email><create>1</create><update>1</update></email>
  9.                 <password><create>1</create></password>
  10.                 <confirmation><create>1</create></confirmation>
  11.                 <dob><create>1</create><update>1</update></dob>
  12.                 <taxvat><create>1</create><update>1</update></taxvat>
  13.                 <occupation><create>1</create><update>1</update></occupation>
  14.             </customer_account>
  15.         </fieldsets>

[/Update 30 Aug 2008 - AlexSz]

[Update 23 Dec 2008 - phlux0r] Instead of editing the core files as described above, I created under app/local/MyCompany the following folders: /Customer /Customer/etc /Customer/Model/Entity /Customer/sql

I copied the original config.xml (as stated above) to the local etc folder and kept only the parts that are to be changed:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <config>
  3.     <modules>
  4.         <MyCompany_Customer>
  5.             <version>0.1</version>
  6.         </MyCompany_Customer>
  7.     </modules>
  8.     <admin>
  9.         <fieldsets>
  10.             <customer_dataflow>
  11.                 <store_id><system>1</system></store_id>
  12.                 <store><ignore>1</ignore></store>
  13.                 <entity_id><system>1</system><ignore>1</ignore></entity_id>
  14.                 <attribute_set_id><system>1</system><ignore>1</ignore></attribute_set_id>
  15.                 <attribute_set><ignore>1</ignore></attribute_set>
  16.                 <entity_type_id><system>1</system></entity_type_id>
  17.                 <increment_id><system>1</system><ignore>1</ignore></increment_id>
  18.                 <parent_id><system>1</system></parent_id>
  19.                 <created_at><system>1</system></created_at>
  20.                 <updated_at><system>1</system></updated_at>
  21.                 <group_id><ignore>1</ignore></group_id>
  22.                 <type_id><system>1</system><ignore>1</ignore></type_id>
  23.                 <type><ignore>1</ignore></type>
  24.                 <created_in><system>1</system><ignore>1</ignore></created_in>
  25.                 <website_id><system>1</system></website_id>
  26.                 <default_billing><system>1</system></default_billing>
  27.                 <default_shipping><system>1</system></default_shipping>
  28.  
  29.                 <prefix><billing>1</billing><shipping>1</shipping><mapped>1</mapped></prefix>
  30.                 <firstname><billing>1</billing><shipping>1</shipping><required>1</required><mapped>1</mapped></firstname>
  31.                 <middlename><billing>1</billing><shipping>1</shipping><mapped>1</mapped></middlename>
  32.                 <lastname><billing>1</billing><shipping>1</shipping><required>1</required><mapped>1</mapped></lastname>
  33.                 <suffix><billing>1</billing><shipping>1</shipping><mapped>1</mapped></suffix>
  34.                 <street_full><billing>1</billing><shipping>1</shipping></street_full>
  35.                 <street1><street>1</street></street1>
  36.                 <street2><street>1</street></street2>
  37.                 <street3><street>1</street></street3>
  38.                 <street4><street>1</street></street4>
  39.                 <street5><street>1</street></street5>
  40.                 <street6><street>1</street></street6>
  41.                 <street7><street>1</street></street7>
  42.                 <street8><street>1</street></street8>
  43.                 <city><billing>1</billing><shipping>1</shipping></city>
  44.                 <region><billing>1</billing><shipping>1</shipping></region>
  45.                 <country><billing>1</billing><shipping>1</shipping><billing_required>1</billing_required><shipping_required>1</shipping_required></country>
  46.                 <postcode><billing>1</billing><shipping>1</shipping><billing_required>1</billing_required><shipping_required>1</shipping_required></postcode>
  47.                 <telephone><billing>1</billing><shipping>1</shipping></telephone>
  48.                 <company><billing>1</billing><shipping>1</shipping></company>
  49.                 <fax><billing>1</billing><shipping>1</shipping></fax>
  50.                 <mobile><billing>1</billing><shipping>1</shipping></mobile>
  51.             </customer_dataflow>
  52.         </fieldsets>
  53.     </admin>
  54.     <global>
  55.         <fieldsets>
  56.             <customer_account>
  57.                 <prefix><create>1</create><update>1</update><name>1</name></prefix>
  58.                 <firstname><create>1</create><update>1</update><name>1</name></firstname>
  59.                 <middlename><create>1</create><update>1</update><name>1</name></middlename>
  60.                 <lastname><create>1</create><update>1</update><name>1</name></lastname>
  61.                 <suffix><create>1</create><update>1</update><name>1</name></suffix>
  62.                 <email><create>1</create><update>1</update></email>
  63.                 <password><create>1</create></password>
  64.                 <confirmation><create>1</create></confirmation>
  65.                 <dob><create>1</create><update>1</update></dob>
  66.                 <taxvat><create>1</create><update>1</update></taxvat>
  67.             </customer_account>
  68.         </fieldsets>
  69.  
  70.         <customer>
  71.             <address>
  72.                 <formats>
  73.                     <text translate="title" module="customer">
  74.                         <title>Text</title>
  75.                         <defaultFormat><![CDATA[
  76. {{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}
  77. {{depend company}}{{var company}}{{/depend}}
  78. {{var street1}}
  79. {{depend street2}}{{var street2}}{{/depend}}
  80. {{depend street3}}{{var street3}}{{/depend}}
  81. {{depend street4}}{{var street4}}{{/depend}}
  82. {{depend city}}{{var city}},  {{/depend}}{{depend region}}{{var region}}, {{/depend}}{{var postcode}}
  83. {{var country}}
  84. T: {{var telephone}}
  85. {{depend fax}}F: {{var fax}}{{/depend}}
  86. {{depend mobile}}M: {{var mobile}}{{/depend}}
  87.                         ]]></defaultFormat>
  88.                     </text>
  89.                     <oneline translate="title" module="customer">
  90.                         <title>Text One Line</title>
  91.                         <htmlEscape>true</htmlEscape>
  92.                         <defaultFormat>
  93. <![CDATA[{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}, {{var street}}, {{var city}}, {{var region}} {{var postcode}}, {{var country}}]]>
  94.                         </defaultFormat>
  95.                     </oneline>
  96.                     <html translate="title" module="customer">
  97.                         <title>HTML</title>
  98.                         <htmlEscape>true</htmlEscape>
  99.                         <defaultFormat><![CDATA[
  100. {{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}<br/>
  101. {{depend company}}{{var company}}<br />{{/depend}}
  102. {{var street1}}<br />
  103. {{depend street2}}{{var street2}}<br />{{/depend}}
  104. {{depend street3}}{{var street3}}<br />{{/depend}}
  105. {{depend street4}}{{var street4}}<br />{{/depend}}
  106. {{depend city}}{{var city}},  {{/depend}}{{depend region}}{{var region}}, {{/depend}}{{var postcode}}<br/>
  107. {{var country}}<br/>
  108. {{depend telephone}}T: {{var telephone}}{{/depend}}
  109. {{depend fax}}<br/>F: {{var fax}}{{/depend}}
  110. {{depend mobile}}<br/>M: {{var mobile}}{{/depend}}
  111.             ]]></defaultFormat>
  112.                     </html>
  113.                     <pdf translate="title" module="customer">
  114.                         <title>PDF</title>
  115.                         <defaultFormat><![CDATA[
  116. {{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}|
  117. {{depend company}}{{var company}}|{{/depend}}
  118. {{var street1}}|
  119. {{depend street2}}{{var street2}}|{{/depend}}
  120. {{depend street3}}{{var street3}}|{{/depend}}
  121. {{depend street4}}{{var street4}}|{{/depend}}
  122. {{depend city}}{{var city}},  {{/depend}}{{depend region}}{{var region}}, {{/depend}}{{var postcode}}|
  123. {{var country}}|
  124. {{depend telephone}}T: {{var telephone}}{{/depend}}|
  125. {{depend fax}}<br/>F: {{var fax}}{{/depend}}|
  126. {{depend mobile}}<br/>M: {{var mobile}}{{/depend}}|
  127.             ]]></defaultFormat>
  128.                     </pdf>
  129.                     <js_template template="title" module="customer">
  130.                         <title>Javascript Template</title>
  131.                         <defaultFormat><![CDATA[#{prefix} #{firstname} #{middlename} #{lastname} #{suffix}<br/>#{company}<br/>#{street0}<br/>#{street1}<br/>#{street2}<br/>#{street3}<br/>#{city}, #{region}, #{postcode}<br/>#{country_id}<br/>T: #{telephone}<br/>F: #{fax}<br/>M: #{mobile}]]></defaultFormat>
  132.                     </js_template>
  133.                 </formats>
  134.             </address>
  135.         </customer>
  136.         <models>
  137.             <customer_entity>
  138.                 <rewrite>
  139.                     <setup>MyCompany_Customer_Model_Entity_Setup</setup>
  140.                 </rewrite>
  141.             </customer_entity>
  142.         </models>
  143.         <resources>
  144.             <customer_setup>
  145.                 <rewrite>
  146.                     <setup>MyCompany_Customer_Model_Entity_Setup</setup>
  147.                 </rewrite>
  148.             </customer_setup>
  149.         </resources>
  150.     </global>
  151. </config>

In the above file, the stuff within the curly brackets { ... } (and specifically the <html...> section) are the customer address templates that are displayed on the Sales Order pages, the Manage Customers > Addresses tabs and on the One page checkout status side bar. The {depend} tags determine whether a field is shown when its value is empty.

I created in Customer/Model/Entity Setup.php with the following content:

  1. class MyCompany_Customer_Model_Entity_Setup extends Mage_Customer_Model_Entity_Setup
  2. {
  3.  
  4.     public function getDefaultEntities()
  5.     {
  6.         return array(
  7.             'customer' => array(
  8.                 'entity_model'          =>'customer/customer',
  9.                 'table'                 => 'customer/entity',
  10.                 'increment_model'       => 'eav/entity_increment_numeric',
  11.                 'increment_per_store'   => false,
  12.                     'website_id' => array(
  13.                         'type'          => 'static',
  14.                         'label'         => 'Associate to Website',
  15.                         'input'         => 'select',
  16.                         'source'        => 'customer/customer_attribute_source_website',
  17.                         'backend'       => 'customer/customer_attribute_backend_website',
  18.                         'sort_order'    => 10,
  19.                     ),
  20.                     'store_id' => array(
  21.                         'type'          => 'static',
  22.                         'label'         => 'Create In',
  23.                         'input'         => 'select',
  24.                         'source'        => 'customer/customer_attribute_source_store',
  25.                         'backend'       => 'customer/customer_attribute_backend_store',
  26.                         'visible'       => false,
  27.                         'sort_order'    => 20,
  28.                     ),
  29.                     'created_in' => array(
  30.                         'type'          => 'varchar',
  31.                         'label'         => 'Created From',
  32.                         'sort_order'    => 30,
  33.                     ),
  34.                     'prefix' => array(
  35.                         'label'         => 'Prefix',
  36.                         'required'      => false,
  37.                         'sort_order'    => 37,
  38.                     ),
  39.                     'firstname' => array(
  40.                         'label'         => 'First Name',
  41.                         'sort_order'    => 40,
  42.                     ),
  43.                     'middlename' => array(
  44.                         'label'         => 'Middle Name/Initial',
  45.                         'required'      => false,
  46.                         'sort_order'    => 43,
  47.                     ),
  48.                     'lastname' => array(
  49.                         'label'         => 'Last Name',
  50.                         'sort_order'    => 50,
  51.                     ),
  52.                     'suffix' => array(
  53.                         'label'         => 'Suffix',
  54.                         'required'      => false,
  55.                         'sort_order'    => 53,
  56.                     ),
  57.                     'email' => array(
  58.                         'type'          => 'static',
  59.                         'label'         => 'Email',
  60.                         'class'         => 'validate-email',
  61.                         'sort_order'    => 60,
  62.                     ),
  63.                     'group_id' => array(
  64.                         'type'          => 'static',
  65.                         'input'         => 'select',
  66.                         'label'         => 'Customer Group',
  67.                         'source'        => 'customer/customer_attribute_source_group',
  68.                         'sort_order'    => 70,
  69.                     ),
  70.                     'dob' => array(
  71.                         'type'          => 'datetime',
  72.                         'input'         => 'date',
  73.                         'backend'       => 'eav/entity_attribute_backend_datetime',
  74.                         'required'      => false,
  75.                         'label'         => 'Date Of Birth',
  76.                         'sort_order'    => 80,
  77.                     ),
  78.                     'password_hash' => array(
  79.                         'input'         => 'hidden',
  80.                         'backend'       => 'customer/customer_attribute_backend_password',
  81.                         'required'      => false,
  82.                     ),
  83.                     'default_billing' => array(
  84.                         'type'          => 'int',
  85.                         'visible'       => false,
  86.                         'required'      => false,
  87.                         'backend'       => 'customer/customer_attribute_backend_billing',
  88.                     ),
  89.                     'default_shipping' => array(
  90.                         'type'          => 'int',
  91.                         'visible'       => false,
  92.                         'required'      => false,
  93.                         'backend'       => 'customer/customer_attribute_backend_shipping',
  94.                     ),
  95.                     'taxvat' => array(
  96.                         'label'         => 'Tax/GST number',
  97.                         'visible'       => true,
  98.                         'required'      => false,
  99.                         'position'      => 1,
  100.                     ),
  101.                     'confirmation' => array(
  102.                         'label'         => 'Is confirmed',
  103.                         'visible'       => false,
  104.                         'required'      => false,
  105.                     ),
  106.                 ),
  107.  
  108.             ),
  109.  
  110.             'customer_address'=>array(
  111.                 'entity_model'  =>'customer/customer_address',
  112.                 'table' => 'customer/address_entity',
  113.                'is_active'         => array('type'=>'static'),
  114.                     'prefix' => array(
  115.                         'label'         => 'Prefix',
  116.                         'required'      => false,
  117.                         'sort_order'    => 7,
  118.                     ),
  119.                     'firstname' => array(
  120.                         'label'         => 'First Name',
  121.                         'sort_order'    => 10,
  122.                     ),
  123.                     'middlename' => array(
  124.                         'label'         => 'Middle Name/Initial',
  125.                         'required'      => false,
  126.                         'sort_order'    => 13,
  127.                     ),
  128.                     'lastname' => array(
  129.                         'label'         => 'Last Name',
  130.                         'sort_order'    => 20,
  131.                     ),
  132.                     'suffix' => array(
  133.                         'label'         => 'Suffix',
  134.                         'required'      => false,
  135.                         'sort_order'    => 23,
  136.                     ),
  137.                     'company' => array(
  138.                         'label'         => 'Company',
  139.                         'required'      => false,
  140.                         'sort_order'    => 30,
  141.                     ),
  142.                     'street' => array(
  143.                         'type'          => 'text',
  144.                         'backend'       => 'customer_entity/address_attribute_backend_street',
  145.                         'input'         => 'multiline',
  146.                         'label'         => 'Street Address',
  147.                         'sort_order'    => 40,
  148.                     ),
  149.                     'city' => array(
  150.                         'label'         => 'City',
  151.                         'sort_order'    => 50,
  152.                     ),
  153.                     'country_id' => array(
  154.                         'type'          => 'varchar',
  155.                         'input'         => 'select',
  156.                         'label'         => 'Country',
  157.                         'class'         => 'countries',
  158.                         'source'        => 'customer_entity/address_attribute_source_country',
  159.                         'sort_order'    => 60,
  160.                     ),
  161.                     'region' => array(
  162.                         'backend'       => 'customer_entity/address_attribute_backend_region',
  163.                         'label'         => 'State/Province',
  164.                         'class'         => 'regions',
  165.                         'sort_order'    => 70,
  166.                     ),
  167.                     'region_id' => array(
  168.                         'type'          => 'int',
  169.                         'input'         => 'hidden',
  170.                         'source'        => 'customer_entity/address_attribute_source_region',
  171.                         'required'      => 'false',
  172.                         'sort_order'    => 80,
  173.                     ),
  174.                     'postcode' => array(
  175.                         'label'         => 'Zip/Postal Code',
  176.                         'sort_order'    => 90,
  177.                     ),
  178.                     'telephone' => array(
  179.                         'label'         => 'Telephone',
  180.                         'sort_order'    => 100,
  181.                     ),
  182.                     'fax' => array(
  183.                         'label'         => 'Fax',
  184.                         'required'      => false,
  185.                         'sort_order'    => 110,
  186.                     ),
  187.                     'mobile' => array(
  188.                         'label'         => 'Mobile',
  189.                         'required'      => false,
  190.                         'sort_order'    => 120,
  191.                     ),
  192.             ),
  193.         );
  194.     }
  195.  
  196. }

I also created the sql file to update the attribute but I don’t know how to actually get it to run as this is not a standard magento connect module installed through the installer (if anyone knows how to get Magento to run the SQL, let me know :).

I also edited the template files described above. Additionally, I edited the One Page Checkout template files as well: /template/checkout/onepage/billing.phtml - added after the fax field:

  1. <li>
  2.     <div class="input-box">
  3.      <label for="billing:mobile"><?php echo $this->__('Mobile') ?></label><br />
  4.      <input type="text" name="billing[mobile]" id="billing:mobile" value="<?php echo $this->htmlEscape($this->getAddress()->getMobile()) ?>" title="<?php echo $this->__('Mobile') ?>" class="input-text" />
  5.     </div>
  6. </li>

/template/checkout/onepage/shipping.phtml - added after the fax field:

  1. <li>
  2.     <div class="input-box">
  3.     <label for="shipping:mobile"><?php echo $this->__('Mobile') ?></label><br />
  4.     <input type="text" name="shipping[mobile]" id="shipping:mobile" value="<?php echo $this->htmlEscape($this->getAddress()->getMobile()) ?>" title="<?php echo $this->__('Mobile') ?>" class="input-text" onchange="shipping.setSameAsBilling(false);" />
  5.     </div>
  6. </li>

Since we are adding a field to the addresses in order for them to show up on all the admin pages including the Sales/Order pages and for Guest checkouts, we need to add 3 versions of the mobile attribute to the eav_attribute table plus a column to the sales_flat_quote_address table since that one doesn’t follow the eav model and stores the guest checkout address in columns (doh!):

  1. //  Run this once when installing manually (ie. in register.phtml)
  2.     $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
  3.     $setup->addAttribute('customer_address', 'mobile', array(
  4.         'label'    => 'Mobile',
  5.         'visible'  => true,
  6.         'required' => false,
  7.         'position'     => 1,
  8.     ));
  9.     $setup->addAttribute('order_address', 'mobile', array(
  10.         'label'    => 'Mobile',
  11.         'visible'  => true,
  12.         'required' => false,
  13.         'position'     => 1,
  14.     ));
  15.     $setup->addAttribute('quote_address', 'mobile', array(
  16.         'label'    => 'Mobile',
  17.         'visible'  => true,
  18.         'required' => false,
  19.         'position'     => 1,
  20.     ));
  21. try {
  22.     $setup->run("
  23.     ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `mobile` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
  24.     ");
  25. }
  26. catch (Exception $e) {
  27. }

The three addAttribute statements will each add a record to your eav_attribute table. The SQL query at the end will add a column to the sales_flat_quote_address table. However if you run the above in another page like /app/design/frontend/[interface]/[theme]/template/checkout/onepage/billing.phtml the $this object does not extend a class with a getTable method which means that your SQL query wont have a table name and it will fail. One way to get around this is to hardcode the table name in the SQL string.

Finally, I overrode the Mage/Sales/etc/config.xml by creating: /MyCompany/Sales/etc/config.xml with:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <config>
  3.     <global>
  4.         <fieldsets>
  5.             <sales_copy_order_billing_address>
  6.                 <prefix><to_order>*</to_order></prefix>
  7.                 <firstname><to_order>*</to_order></firstname>
  8.                 <middlename><to_order>*</to_order></middlename>
  9.                 <lastname><to_order>*</to_order></lastname>
  10.                 <suffix><to_order>*</to_order></suffix>
  11.                 <customer_address_id><to_order>*</to_order></customer_address_id>
  12.                 <company><to_order>*</to_order></company>
  13.                 <street><to_order>*</to_order></street>
  14.                 <city><to_order>*</to_order></city>
  15.                 <region><to_order>*</to_order></region>
  16.                 <postcode><to_order>*</to_order></postcode>
  17.                 <country_id><to_order>*</to_order></country_id>
  18.                 <telephone><to_order>*</to_order></telephone>
  19.                 <fax><to_order>*</to_order></fax>
  20.                 <mobile><to_order>*</to_order></mobile>
  21.                 <region_id><to_order>*</to_order></region_id>
  22.             </sales_copy_order_billing_address>
  23.             <sales_copy_order_shipping_address>
  24.                 <prefix><to_order>*</to_order></prefix>
  25.                 <firstname><to_order>*</to_order></firstname>
  26.                 <middlename><to_order>*</to_order></middlename>
  27.                 <lastname><to_order>*</to_order></lastname>
  28.                 <suffix><to_order>*</to_order></suffix>
  29.                 <customer_address_id><to_order>*</to_order></customer_address_id>
  30.                 <company><to_order>*</to_order></company>
  31.                 <street><to_order>*</to_order></street>
  32.                 <city><to_order>*</to_order></city>
  33.                 <region><to_order>*</to_order></region>
  34.                 <postcode><to_order>*</to_order></postcode>
  35.                 <country_id><to_order>*</to_order></country_id>
  36.                 <telephone><to_order>*</to_order></telephone>
  37.                 <fax><to_order>*</to_order></fax>
  38.                 <mobile><to_order>*</to_order></mobile>
  39.                 <region_id><to_order>*</to_order></region_id>
  40.             </sales_copy_order_shipping_address>
  41.             <sales_convert_quote_address>
  42.                 <weight><to_order>*</to_order></weight>
  43.                 <shipping_method><to_order>*</to_order></shipping_method>
  44.                 <shipping_description><to_order>*</to_order></shipping_description>
  45.                 <shipping_rate><to_order>*</to_order></shipping_rate>
  46.  
  47.                 <subtotal><to_order>*</to_order></subtotal>
  48.                 <tax_amount><to_order>*</to_order></tax_amount>
  49.                 <tax_string><to_order>*</to_order></tax_string>
  50.                 <discount_amount><to_order>*</to_order></discount_amount>
  51.                 <shipping_amount><to_order>*</to_order></shipping_amount>
  52.                 <shipping_tax_amount><to_order>*</to_order></shipping_tax_amount>
  53.                 <giftcert_amount><to_order>*</to_order></giftcert_amount>
  54.                 <custbalance_amount><to_order>*</to_order></custbalance_amount>
  55.                 <grand_total><to_order>*</to_order></grand_total>
  56.  
  57.                 <base_subtotal><to_order>*</to_order></base_subtotal>
  58.                 <base_tax_amount><to_order>*</to_order></base_tax_amount>
  59.                 <base_discount_amount><to_order>*</to_order></base_discount_amount>
  60.                 <base_shipping_amount><to_order>*</to_order></base_shipping_amount>
  61.                 <base_shipping_tax_amount><to_order>*</to_order></base_shipping_tax_amount>
  62.                 <base_giftcert_amount><to_order>*</to_order></base_giftcert_amount>
  63.                 <base_custbalance_amount><to_order>*</to_order></base_custbalance_amount>
  64.                 <base_grand_total><to_order>*</to_order></base_grand_total>
  65.  
  66.                 <prefix><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></prefix>
  67.                 <firstname><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></firstname>
  68.                 <middlename><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></middlename>
  69.                 <lastname><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></lastname>
  70.                 <suffix><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></suffix>
  71.                 <company><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></company>
  72.                 <street_full><to_order_address>street</to_order_address></street_full>
  73.                 <street><to_customer_address>*</to_customer_address></street><!-- ^^^ ? -->
  74.                 <city><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></city>
  75.                 <region><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></region>
  76.                 <region_id><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></region_id>
  77.                 <postcode><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></postcode>
  78.                 <country_id><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></country_id>
  79.                 <telephone><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></telephone>
  80.                 <fax><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></fax>
  81.                 <mobile><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></mobile>
  82.                 <email><to_customer_address>*</to_customer_address></email>
  83.             </sales_convert_quote_address>
  84.             <sales_convert_order_address>
  85.                 <prefix><to_quote_address>*</to_quote_address></prefix>
  86.                 <firstname><to_quote_address>*</to_quote_address></firstname>
  87.                 <middlename><to_quote_address>*</to_quote_address></middlename>
  88.                 <lastname><to_quote_address>*</to_quote_address></lastname>
  89.                 <suffix><to_quote_address>*</to_quote_address></suffix>
  90.                 <company><to_quote_address>*</to_quote_address></company>
  91.                 <street_full><to_quote_address>street</to_quote_address></street_full>
  92.                 <city><to_quote_address>*</to_quote_address></city>
  93.                 <region><to_quote_address>*</to_quote_address></region>
  94.                 <region_id><to_quote_address>*</to_quote_address></region_id>
  95.                 <postcode><to_quote_address>*</to_quote_address></postcode>
  96.                 <country_id><to_quote_address>*</to_quote_address></country_id>
  97.                 <telephone><to_quote_address>*</to_quote_address></telephone>
  98.                 <fax><to_quote_address>*</to_quote_address></fax>
  99.                 <mobile><to_quote_address>*</to_quote_address></mobile>
  100.             </sales_convert_order_address>
  101.             <customer_address>
  102.                 <id><to_quote_address>customer_address_id</to_quote_address></id>
  103.                 <parent_id><to_quote_address>customer_id</to_quote_address></parent_id>
  104.                 <prefix><to_quote_address>*</to_quote_address></prefix>
  105.                 <firstname><to_quote_address>*</to_quote_address></firstname>
  106.                 <middlename><to_quote_address>*</to_quote_address></middlename>
  107.                 <lastname><to_quote_address>*</to_quote_address></lastname>
  108.                 <suffix><to_quote_address>*</to_quote_address></suffix>
  109.                 <!--<email><to_quote_address>*</to_quote_address></email>-->
  110.                 <company><to_quote_address>*</to_quote_address></company>
  111.                 <street_full><to_quote_address>street</to_quote_address></street_full>
  112.                 <city><to_quote_address>*</to_quote_address></city>
  113.                 <region><to_quote_address>*</to_quote_address></region>
  114.                 <region_id><to_quote_address>*</to_quote_address></region_id>
  115.                 <postcode><to_quote_address>*</to_quote_address></postcode>
  116.                 <country_id><to_quote_address>*</to_quote_address></country_id>
  117.                 <telephone><to_quote_address>*</to_quote_address></telephone>
  118.                 <fax><to_quote_address>*</to_quote_address></fax>
  119.                 <mobile><to_quote_address>*</to_quote_address></mobile>
  120.             </customer_address>
  121.         </fieldsets>
  122.     </global>
  123. </config>

Also, not to forget to enable your overridden modules in: app/etc/modules/MyCompany_All.xml:

  1.         <MyCompany_Customer>
  2.             <active>true</active>
  3.             <codePool>local</codePool>
  4.         </MyCompany_Customer>
  5.         <MyCompany_Sales>
  6.             <active>true</active>
  7.             <codePool>local</codePool>
  8.         </MyCompany_Sales>

[/Update 23 Dec 2008 - phlux0r]




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs