Magento – Display custom field in customer address using address template

addresscustom-optionsmagento-1.8

I tried to display a custom field in the customer address using "Address Templates" from admin interface, but it doesn't work. Here what I did:

I installed this extension to manage the fiscal code on Magento. This extension use the following configuration (etc/config.xml):

<?xml version="1.0"?>
<config>
<modules>
    <BHuman_TaxCodeAndPrivacy>
        <version>0.1.4</version>
    </BHuman_TaxCodeAndPrivacy>
</modules>
<global>
  <fieldsets>
      <checkout_onepage_billing>            
                <customer_taxcode><to_customer>taxcode</to_customer>       </customer_taxcode>
      </checkout_onepage_billing>

      <customer_account>
            <taxcode><create>1</create><update>1</update><to_quote>customer_taxcode</to_quote><to_order>customer_taxcode</to_order></taxcode>
            <privacy><create>1</create><update>1</update></privacy>
      </customer_account>


  </fieldsets>
    <blocks>
        <taxcodeandprivacy>
                <class>BHuman_TaxCodeAndPrivacy_Block</class>
        </taxcodeandprivacy>
    </blocks>
      <models>
      <customer>
          <rewrite>
              <customer>BHuman_TaxCodeAndPrivacy_Model_Customer</customer>
          </rewrite>
      </customer>
      <sales>
          <rewrite>
              <order_pdf_invoice>BHuman_TaxCodeAndPrivacy_Model_Sales_Order_Pdf_Invoice</order_pdf_invoice>
              <order_pdf_creditmemo>BHuman_TaxCodeAndPrivacy_Model_Sales_Order_Pdf_Creditmemo</order_pdf_creditmemo>
          </rewrite>
      </sales>
      <checkout>
          <rewrite>
              <type_onepage>BHuman_TaxCodeAndPrivacy_Model_Checkout_Type_Onepage</type_onepage>
          </rewrite>
      </checkout>    
  </models>
    <resources>
      <taxcodeandprivacy_setup>
            <setup>
                    <module>BHuman_TaxCodeAndPrivacy</module>
                    <class>BHuman_TaxCodeAndPrivacy_Model_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
       </taxcodeandprivacy_setup>
      <taxcodeandprivacy_write>
          <connection>
                <use>core_write</use>
            </connection>
      </taxcodeandprivacy_write>
      <taxcodeandprivacy_read>
            <connection>
                 <use>core_read</use>
            </connection>
      </taxcodeandprivacy_read>
    </resources>
    <helpers>
        <taxcodeandprivacy>
            <class>BHuman_TaxCodeAndPrivacy_Helper</class>
        </taxcodeandprivacy>
    </helpers>
</global>
<frontend>
    <layout>
        <updates>
            <taxcodeandprivacy>
                <file>taxcodeandprivacy.xml</file>
            </taxcodeandprivacy>
        </updates>
    </layout>
  <translate>
      <modules>
          <BHuman_TaxCodeAndPrivacy>
              <files>
                  <default>BHuman_TaxCodeAndPrivacy.csv</default>
              </files>
          </BHuman_TaxCodeAndPrivacy>
      </modules>
  </translate>  
</frontend>
<default>
    <customer>
        <address>
            <taxcode_show>0</taxcode_show>
            <privacy_show>0</privacy_show>
        </address>
    </customer>
</default>  

following this guide I added the following lines

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

and

                <sales_convert_quote>
                <customer_taxcode><to_order>*</to_order></customer_taxcode>
            </sales_convert_quote>
            <sales_convert_order>
                <customer_taxcode><to_quote>*</to_quote></customer_taxcode>
            </sales_convert_order>

            <sales_copy_order_billing_address>
                <taxcode><to_order>*</to_order></taxcode>
            </sales_copy_order_billing_address>

            <sales_copy_order_shipping_address>
            <taxcode><to_order>*</to_order></taxcode>
          </sales_copy_order_shipping_address>

          <sales_convert_quote_address>
            <taxcode><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></taxcode>
          </sales_convert_quote_address>

          <sales_convert_order_address>
            <taxcode><to_quote_address>*</to_quote_address></taxcode>
          </sales_convert_order_address>

          <customer_address>
            <taxcode><to_quote_address>*</to_quote_address></taxcode>
          </customer_address>

          <checkout_onepage_billing>
            <taxcode><to_customer>*</to_customer></taxcode>
          </checkout_onepage_billing>

          <checkout_onepage_quote>
            <taxcode><to_customer>*</to_customer></taxcode>
          </checkout_onepage_quote>

then in the admin interface, in system -> configuration -> customer configuration -> address template I added the taxcode field in this way:

...
CF: {{var taxcode}}
...

I'm getting crazy but id doesn't work, in the address on frontend or email template I can see only CF without the value… Any idea?

Best Answer

So normally the address is formatted with the following line of code $address->format('html'); where html is the format.

Basically this will work its way down to the function render in Mage_Customer_Block_Address_Renderer_Default. This will loop through add the address attributes and build a data array to be used. It does check that the address attribute is visible via if (!$attribute->getIsVisible()) {

I would suggest trying two things.

  1. Firstly check that the attribute has is_visible set,

  2. Then check that the value is set when the address is formatted and if not it can be set using an observer,

    There is an event customer_address_format which gives you the $type of the formatting and the $address object. When listening to this event if the attribute is not set you could easily set the value for use later.