Woocommerce is a most common platform used by many companies to sell their goods online. The most important feature of this WordPress plugin is ease of use and ability to extend. This is blog post you will learn how you can change the address format according to your requirements or for a specific country.
Address formats define the way Billing and Shipping addresses are formatted for the end user. Addresses show on the order received / thank you page, on transactional emails and on some My Account pages, and look like this:
{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}
You client may ask you to move “company’ name before customer’s name or they may ask you to change the address 1 and address 2 in one line. For example in Canada, Postal code is usually written at the end of address. So, “woocommerce_localisation_address_formats” filter allows you to modify address format as your client’s requirements.
/** * @snippet Modify Billing/Shipping Address * @author labotrees * @compatible WooCommerce 7 */ add_filter( 'woocommerce_localisation_address_formats', 'labotrees_address_reformat ); function labotrees_address_reformat( $address ) { $address['CA'] = "{company}\n{name}\n{address_1}, {address_2}\n{city}\n{state} {country}\n{postcode}"; return $address; }
The “\n” adds a new line to address. You can add anything commas, dots, spaces. In the example above, for the CA country, I’ve moved “company” to the first line, left “name” at same place.
The “address 1” and “address 2” are placed on the same line separated by a comma, then “city“, then “state” and “country” on the same line with a blank space in between, and finally the “postcode“. Enjoy…