You can change address format defined by default in the in app/code/core/Mage/Customer/config.xml
. Go to System > Configuration > Customers > Customer Configuration > Address Templates
and add the custom attribute where you want. Result should looks like this:
{{depend store_id}}Store ID{{var store_id}}{{/depend}}
{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}
{{depend company}}{{var company}}{{/depend}}
{{if street1}}{{var street1}}
{{/if}} ....
You can find additional info here (pt. 3-5)
Note: The attribute should be present in the sales/order_address
and sales/quote_address
, in order to display correctly on all the stages.
Example:
Add attribute:
$installer = $this;
$installer->startSetup();
$this->addAttribute('customer_address', 'custom_store_id', array(
'type' => 'text',
'input' => 'text',
'label' => 'Custom Store No#',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
->getAttribute('customer_address', 'custom_store_id')
->setData('used_in_forms', array('customer_register_address','customer_address_edit','adminhtml_customer_address'))
->save();
/**
* Adding Extra Column to sales_flat_order_address
* to store the delivery instruction field
*/
$sales_order_address = $installer->getTable('sales/order_address');
$installer->getConnection()
->addColumn($sales_order_address, 'custom_store_id', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'comment' => 'Custom Store Id'
));
$sales_quote_address = $installer->getTable('sales/quote_address');
$installer->getConnection()
->addColumn($sales_quote_address, 'custom_store_id', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'comment' => 'Custom Store Id'
));
$installer->endSetup();
Add fieldset:
<fieldsets>
<sales_convert_quote_address>
<custom_store_id>
<to_order_address>*</to_order_address>
</custom_store_id>
</sales_convert_quote_address>
</fieldsets>
Modify format:
Before {{depend custom_store_id}}Store ID{{var custom_store_id}} After
{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}<br/>
{{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}}
{{depend vat_id}}<br/>VAT: {{var vat_id}}{{/depend}}
Get result:
Update:
Variables to the template come from the Mage_Core_Model_Email_Template::getProcessedTemplate()
:
/**
* Process email template code
*
* @param array $variables
* @return string
*/
public function getProcessedTemplate(array $variables = array())
{
$processor = $this->getTemplateFilter();
$processor->setUseSessionInUrl(false)
->setPlainTemplateMode($this->isPlain());
if (!$this->_preprocessFlag) {
$variables['this'] = $this;
}
if (isset($variables['subscriber']) && ($variables['subscriber'] instanceof Mage_Newsletter_Model_Subscriber)) {
$processor->setStoreId($variables['subscriber']->getStoreId());
}
// Apply design config so that all subsequent code will run within the context of the correct store
$this->_applyDesignConfig();
// Populate the variables array with store, store info, logo, etc. variables
$variables = $this->_addEmailVariables($variables, $processor->getStoreId());
$processor
->setTemplateProcessor(array($this, 'getTemplateByConfigPath'))
->setIncludeProcessor(array($this, 'getInclude'))
->setVariables($variables);
try {
// Filter the template text so that all HTML content will be present
$result = $processor->filter($this->getTemplateText());
// If the {{inlinecss file=""}} directive was included in the template, grab filename to use for inlining
$this->setInlineCssFile($processor->getInlineCssFile());
// Now that all HTML has been assembled, run email through CSS inlining process
$processedResult = $this->getPreparedTemplateText($result);
}
catch (Exception $e) {
$this->_cancelDesignConfig();
throw $e;
}
$this->_cancelDesignConfig();
return $processedResult;
}
where the $processor->filter($this->getTemplateText());
calls callback functions (methods) one by one and replace a clause "{{...}}" by the result.
Address render comes from the Mage_Customer_Block_Address_Renderer_Default::render()
. Customer attributes comes from the Mage_Customer_Helper_Address::getAttributes()
:
$attributes = Mage::helper('customer/address')->getAttributes();
So, if you get your attribute in the array from the method Mage_Customer_Helper_Address::getAttributes()
, you can call it in the address template using this: {{var attributeName}}
. The Mage_Core_Model_Email_Template_Filter::varDirective()
method collects all variables and stores them in the array _templateVars
. Method Varien_Filter_Template::_getVariable()
finally replaces a variables from the _templateVars
array.