There are easier ways to format Customer addresses in Magento than to dig in code and code in line...
Default
If you want to add a default address format regardless of address country then you simply need to add codes to your config.xml as follows:
UPDATE
<config>
<default>
<customer>
<address_templates>
<text><![CDATA[{{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}}
{{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}}]]></text>
<oneline><![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}}]]></oneline>
<html><![CDATA[{{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}}]]></html>
<pdf><![CDATA[{{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}}
{{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}}|]]></pdf>
<js_template><![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}]]></js_template>
</address_templates>
</customer>
</default>
</config>
By Country
There is an easy way to add the above formatting based on each country, (for instance addresses in France and Germany needs to have postcodes before city names). All you need to do is to add in DB table 'directory_country_format' the country id (2 digit country ID), type (html/text/pdf) and the format text. An Example is given below:
INSERT INTO `directory_country_format` (`country_id`, `type`, `format`) VALUES
('FR', 'html', '{{depend company}}{{var company}}
{{/depend}}\n{{var firstname}} {{var lastname}}
\n{{var street1}}
\n{{depend street2}}{{var street2}}
{{/depend}}\n{{depend postcode}}{{var postcode}}{{/depend}}{{depend city}} {{var city}}{{/depend}}{{depend region}}, {{var region}}{{/depend}}
\n{{var country}}
\n{{depend telephone}}Tel : {{var telephone}}{{/depend}}\n{{depend fax}}
Fax : {{var fax}}{{/depend}}'),
('FR', 'text', '{{depend company}}{{var company}}\n{{/depend}}{{var firstname}} {{var lastname}}\n{{var street1}}{{depend street2}}{{var street2}}\n{{/depend}}\n{{depend postcode}}{{var postcode}}{{/depend}}{{depend city}} {{var city}}{{/depend}}{{depend region}}, {{var region}}{{/depend}}\n{{var country}}{{depend telephone}}\nTel : {{var telephone}}{{/depend}}{{depend fax}}\nFax : {{var fax}}{{/depend}}'),
('FR', 'pdf', '{{depend company}}{{var company}}|\n{{/depend}}{{var firstname}} {{var lastname}}|\n{{var street1}}|{{depend street2}}{{var street2}}|\n{{/depend}}\n{{depend postcode}}{{var postcode}}{{/depend}}{{depend city}} {{var city}}{{/depend}}{{depend region}}, {{var region}}{{/depend}}|\n{{var country}}{{depend telephone}}|\nTel : {{var telephone}}{{/depend}}{{depend fax}}|\nFax : {{var fax}}{{/depend}}|');
Special cases / formatting
In some cases we might want to do special formatting to certain data on address object so that they are printed out in the format the way we want (for instance we might want to print the country name or the last name in uppercase). That is achievable using observers on event 'customer_address_format'. And your observer can be like the example below:
class MyModule_Model_Countryformat
{
public function countryFormat($observer)
{
$event = $observer->getEvent();
$address = $event->getAddress();
$type = $event->getType();
if($address->getData('country_id') == 'FR'){
$lastname = strtoupper($address->getData('lastname'));
$address->setData('lastname', $lastname);
}
}
}
You can even add your own address format tags other than the defined html, pdf etc... in order to do so you need to add the following in your config.xml of your module, where mytag is your new tag:
<config>
<global>
<customer>
<address>
<formats>
<mytag template="title" module="customer">
<title>My custom address template</title>
</mytag>
</formats>
</address>
</customer>
</global>
</config>
and then can simply add your format in the same config file
<config>
<default>
<customer>
<address_templates>
<mytag><![CDATA[{{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}}
{{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}}]]></mytag>
</address_templates>
</customer>
</default>
</config>
Thanks, that was my highlight of the day, exactly what I had been looking for.
ReplyDeleteGood Tweak!
ReplyDeleteit help me thx!
ReplyDeleteNice tips on Magento ,Thanks,
ReplyDeleteOk, so I like this idea of using an observer.
ReplyDeleteDo you think this could be used to remove customer first and last name on a PDF, if company exists, without rewriting the PDF's?
To answer my own question.. This is an excellent resource when used in combination with the Inchoo resource.
ReplyDeleteThanks alot ! :)
Delete