In this example I’m going to explain how to add a new customer attribute and collect the data from the checkout attribute or register form.
Remember to activate the Vat/Tax attribute for the checkout, check right image
The new attribute will be a TaxVat Document Type Info, so using the TaxVat attribute of Magento you could collect the kind of document: Passport, NIT, NIF, Cedula, DNI…
1. Add the new customer attribute to the database. Also create a new column to save the attribute when in the quote. (This allows you to keep the value through the onepage checkout page steps).
Execute this code in any .phtml file or any file that you could run under Magento classes.
//Attribute to add
$newAttributeName = "taxvattype"; //modify this with the name of your attribute
//a) Add EAV Attributes (modify as you needed)
$attribute = array(
'type' => 'varchar',
'label' => 'Tax/VAT Type',
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
);
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
//Add to customer
$setup->addAttribute('customer', $newAttributeName, $attribute);
//b) Add Quote attributes (one page step to step save field)
$setup = new Mage_Sales_Model_Mysql4_Setup('sales_setup');
$setup->getConnection()->addColumn(
$setup->getTable('sales_flat_quote'),
$newAttributeName,
'text NULL DEFAULT NULL'
);
$setup->addAttribute('quote', "customer_".$newAttributeName, array('type' => 'static', 'visible' => false));
2. Add a new line to the file
/app/code/core/Mage/Customer/etc/config.xml
<global>
<fieldsets>
<customer_account>
<prefix><create>1</create><update>1</update><name>1</name></prefix>
<firstname><create>1</create><update>1</update><name>1</name></firstname>
<middlename><create>1</create><update>1</update><name>1</name></middlename>
<lastname><create>1</create><update>1</update><name>1</name></lastname>
<suffix><create>1</create><update>1</update><name>1</name></suffix>
<email><create>1</create><update>1</update></email>
<password><create>1</create></password>
<confirmation><create>1</create></confirmation>
<dob><create>1</create><update>1</update></dob>
<taxvat><create>1</create><update>1</update></taxvat>
<taxvattype><create>1</create><update>1</update></taxvattype>
<gender><create>1</create><update>1</update></gender>
3. Modify your checkout template
/app/design/frontend/your_template/default/template/checkout/onepage/billing.phtml
<?php $_taxvat = $this->getLayout()->createBlock('customer/widget_taxvat') ?>
<?php if ($_taxvat->isEnabled()): ?>
<li>
<div class="field">
<label for="billing:taxvattype"><?php echo $this->__('Tax/VAT Document Type') ?></label>
<div class="input-box">
<input type="text" title="<?php echo $this->__('Tax/VAT Document Type') ?>" name="billing[taxvattype]" id="billing:taxvattype" value="<?php echo $this->htmlEscape($this->getData("taxvattype")); ?>" class="input-text" />
</div>
</div>
</li>
<li>
<?php echo $_taxvat->setTaxvat($this->getQuote()->getCustomerTaxvat())->setFieldIdFormat('billing:%s')->setFieldNameFormat('billing[%s]')->toHtml() ?>
</li>
<?php endif ?>
4. Modify the register form template
/app/design/frontend/your_template/default/template/customer/form/register.phtml
<?php if ($_taxvat->isEnabled()): ?>
<div class="input-box">
<label for="taxvat_type"><?php echo $this->__('Tax/VAT Document Type') ?></label><br/>
<input type="text" name="taxvattype" id="taxvattype" value="<?php echo $this->htmlEscape($this->getData("taxvattype")); ?>" title="<?php echo $this->__('Tax/VAT Document Type') ?>" class="input-text" />
</div>
<li><?php echo $_taxvat->setTaxvat($this->getFormData()->getTaxvat())->toHtml() ?></li>
<?php endif ?>
5. Modify the file
/app/code/core/Mage/Checkout/Model/Type/Onepage.php (Please remember to overrite with a module instead).
a) Function saveBilling($data, $customerAddressId)
if (empty($data)) {
return array('error' => -1, 'message' => $this->_helper->__('Invalid data.'));
}
$address = $this->getQuote()->getBillingAddress();
$this->getQuote()->setData("customer_taxvattype",$data['taxvattype']);
if (!empty($customerAddressId)) {
$customerAddress = Mage::getModel('customer/address')->load($customerAddressId);
if ($customerAddress->getId()) {
if ($customerAddress->getCustomerId() != $this->getQuote()->getCustomerId()) {
b) Function saveOrder()
default:
$this->_prepareCustomerQuote();
break;
}
$billing = $this->getQuote()->getBillingAddress();
if ($this->getQuote()->getData("customer_taxvattype") && !$billing->getData("taxvattype")) {
//Save in the customer
$this->getQuote()->getCustomer()->setData("taxvattype", $this->getQuote()->getData("customer_taxvattype"));
}
$service = Mage::getModel('sales/service_quote', $this->getQuote());
$service->submitAll();
- To use the example you have to activate the VatTax attribute for the onepage checkout. Check the image bellow.
- Tested on Magento 1.4.1.1



Sam Ellis
on May 25th, 2011
@ 11:24 am:
Awesome tutorial, it works great except I can’t get it to save my custom attributes in Magento 1.5? It creates the attributes in the database just fine but never seems to write them. Any ideas?
Thanks!
Sam
admin
on May 28th, 2011
@ 10:23 am:
Hi Sam,
I didn’t test it on 1.5 sorry, I recommend you to check $this->getQuote() in app/code/core/Mage/Checkout/Model/Type/Onepage.php (Step5 of this tutorial)
You have to make sure is saving the new attribute value in the quote during the process.
Regards
Zoran
on May 30th, 2011
@ 8:48 pm:
Thanks for the nice tutorial, I have tested it on Magento 1.4.0.1 and it works perfect, still have to implement it in the production… in case something goes wrong, removing the column or attribute from the database afterward shouldn’t cause any side effects, I hope, or?
admin
on Jun 7th, 2011
@ 7:20 pm:
No really, just backup your database first and write it down the changes you do, so you could revert it back.
Best regards
Boris
on Jun 8th, 2011
@ 8:27 am:
Hello,
does anyone have this code in module version working in Magento 1.5.1?
In case you have, please place a link to the zip file with all files that module contains.
Than you in advanced!
FreddyRabbit
on Jun 21st, 2011
@ 10:43 pm:
Thank you for this tutorial. It helped me quite a bit!
I do need to mention that I had to manually enter few records into the DB and them it worked for me….
in customer_form_attribute … add:
customer_account_edit (the new attribute_id)
customer_address_edit (the new attribute_id)
customer_register_address (the new attribute_id)
After I did this it worked in 1.4 and 1.5
Thank you again
pepper
on Jun 30th, 2011
@ 12:34 pm:
Thank you for this tutorial:) But could you please explain the Step 1 to me? Do I have to create a new column in database? In which table? And the code you mention, in which file can I find it?
wisam
on Jul 7th, 2011
@ 1:36 pm:
Hello every body i want to save custom filed in sales_flat_order
i could ceate the filed and the attribute but i couldn’t save the static value in the field any one can help me please
Manish
on Oct 10th, 2011
@ 8:45 am:
I have written a detailed blog here on how to add steps to magento onepage checkout http://www.excellencemagentoblog.com/magento-onestep-checkout-add-step