How to: Change State Name to Postal Abbreviation on WooCommerce Checkout Page using Hook

Advertisement

This is another snippet on how to change State name to Postal Abbreviation in WooCommerce programmatically or using Hook, so you can make “Australian Capital Territory” display as “ACT” or “New South Wales” display as “NSW” and so on, while digging I found few in the net with same topic but I can say they don’t know what they’re doing, yaay they touch core file which they should not as it overwrite and wipe the changes every time WooCommerce release new updates.

This is helpful if you want to print mailing labels for your shipments which only accept Postal Abbreviation, so you don’t have to change it manually and search for correct abbreviation just to make sure you got the right one.

So much for that, let’s get started.

Code

Add the below code into your current themes functions.php file


add_filter( 'woocommerce_states', 'rs_customize_woocommerce_states' );
/**
 * @desc	Update State Name to Postal Abbreviation
 */
function rs_customize_woocommerce_states() {
	global $states;

	$states['AU'] = array(
		'ACT' => __( 'ACT', 'woocommerce' ),
		'NSW' => __( 'NSW', 'woocommerce' ),
		'NT'  => __( 'NT', 'woocommerce' ),
		'QLD' => __( 'QLD', 'woocommerce' ),
		'SA'  => __( 'SA', 'woocommerce' ),
		'TAS' => __( 'TAS', 'woocommerce' ),
		'VIC' => __( 'VIC', 'woocommerce' ),
		'WA'  => __( 'WA', 'woocommerce' )
	);

	$states['US'] = array(
		'AL' => __( 'AL', 'woocommerce' ),
		'AK' => __( 'AK', 'woocommerce' ),
		'AZ' => __( 'AZ', 'woocommerce' ),
		'AR' => __( 'AR', 'woocommerce' )
	)

	...

	// you can find lists of states in WooCommerce plugin directory or in /plugins/woocommerce/i18n/states

	return $states;
}

That’s it, Happy Coding ^_^

Advertisement