Add custom attribute to customer address then use it when creating order in Magento

Add custom attribute to customer address then use it when creating order in Magento

Posted: 5 years ago in  Magento | MySQL | PHP |


In Magento, one customer has 2 default address, one for billing and one for shipping address. The information seem to fit most general address over the world. However, in some countries, the address structure is more complicated


Here is default attributes for customer's address

Our purpose here is adding in more fields as Building, Crossstreet. It would save in default billing and shipping address and when order is created, these information will be fetched automatically. In addition, we are able to update them later if customer change their mind.

On Magento Connect, there is free plugin help us to do this request but it is not perfect result if Magento version is 1.8+.The method I chose is the combination of this Magento plugin and some mofication in xml.

Here is my reason: the plugin named "Manage Category, Customer and Customer Address Attributes" is only compatible up to Magento 1.7, fortunatly, it is also compatible with Magento 1.9. However, in Magento 1.9, when order is created for one customer which has default billing and shipping address, the custom attributes/fields (Building, Crossstreet...) created with this plugin is not filled in even values are set as default in Customer's address information.

After look and tried several ways, I found the way to fix the issue by using the combination.

Following these steps to create attribute building in customer's address (Magento 1.9):

Step 1:

First of all, download and install this plugin using Magento Connect.

Then create custom attribute using admin cpanel Attribute Manager => Customer Address Attribute

Step 2:

Go to magento-folder and open this file: app/code/core/Mage/Customer/etc/config.xml
Search blocks <html> <pdf> and <text> to add in each block just right after "company" line

{{depend building}}Building:{{var building}}<br/>{{/depend}}

For PDF block, don't forget to add | at the end, this is delimiter to separate address attribute when printing invoice PDF.

Create file in app/etc/modules/Tdg_All.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Tdg_Check>
            <active>true</active>
            <codePool>local</codePool>
        </Tdg_Check>
    </modules>
</config>

Open/Create this file: app/code/local/Tdg/Check/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Tdg_Check>
            <version>1.0.1</version>
        </Tdg_Check>
    </modules>
    <admin>
        <fieldsets>
            <customer_dataflow>
                <building><billing>1</billing><shipping>1</shipping></building>
            </customer_dataflow>
        </fieldsets>
    </admin>
    <global>
        <models>
            <check>
                <class>Tdg_Check_Model</class>
            </check>
        </models>
        <resources>
            <check_setup>
                <setup>
                    <module>Tdg_Check</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </check_setup>
            <check_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </check_write>
            <check_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </check_read>
        </resources>
        <fieldsets>
            <sales_copy_order_billing_address>
                <building><to_order>*</to_order></building>
            </sales_copy_order_billing_address>
            <sales_copy_order_shipping_address>
                <building><to_order>*</to_order></building>
            </sales_copy_order_shipping_address>
            <sales_convert_quote_address>
                <building><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></building>
            </sales_convert_quote_address>
            <sales_convert_order_address>
                <building><to_quote_address>*</to_quote_address></building>
            </sales_convert_order_address>
            <customer_address>
                <building><to_quote_address>*</to_quote_address></building>
            </customer_address>
            <checkout_onepage_billing>
                <building><to_customer>*</to_customer></building>
            </checkout_onepage_billing>
        </fieldsets>
    </global>
</config>

Then open mysql CLI or phpMyAdmin as you favorite MySQL client :) run 2 queries:

ALTER TABLE `sales_flat_quote_address` ADD COLUMN `building` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL;
ALTER TABLE `sales_flat_order_address` ADD COLUMN `building` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL;

Step 3:

Sign out from admin cpanel

Delete everything in magento-website-folder/var/cache and magento-website-folder/var/session

Step 4:

Login and check the new field in customer address