Search code examples
phpwordpresswoocommerceformathook-woocommerce

WooCommerce custom localization address format for specific country


I am printing address labels from WooCommerce and find that the line breaks for the addresses are not as I need them.

I found the code I need to change in woocommerce_localisation_address_formats hook and have added the line break but I do not know enough to add the correct code to the functions file thus avoiding going back to the old format on the next update.

This is the corrected replacement line:

'AU' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state} {postcode}\n{country}",

Any help is appreciated.


Solution

  • You need to use woocommerce_localisation_address_formats filter hook as follows:

    add_filter( 'woocommerce_localisation_address_formats', 'filter_localisation_address_formats' );
    function filter_localisation_address_formats( $address_formats ){
        $address_formats['AU'] = "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state} {postcode}\n{country}";
    
        return $address_formats;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.