WooCommerce My Account 页面默认地址格式是姓名、公司、地址 1 、地址 2 、城市、省/州、邮编和国家,更改这个格式需要先更新格式字符串,然后获取所有需要替换的数据,具体方法如下所示。

例如,只显示姓名、电子邮箱和电话,代码放在主题的 functions.php 里。 WooCommerce 2.1.9 下测试。

// 更新地址格式字符串
// 默认是 {name}
{company}
{address_1}
{address_2}
{city}
{state}
{postcode}
{country}
add_filter( 'WooCommerce_localisation_address_formats', 'wc_my_account_address_formats' );
function wc_my_account_address_formats( $formats ){
	$formats['default'] = "{name}
{email}
{phone}";
	return $formats; 
}

// 决定显示哪些数据
// email 和 phone 是新添加的,需要用 get_user_meta 获取值
add_filter( 'WooCommerce_my_account_my_address_formatted_address', 'wc_my_account_custom_formatted_address', 10, 3);
function wc_my_account_custom_formatted_address( $address, $customer_id, $name ){

	$new_address['first_name'] = $address['first_name'];
	$new_address['last_name'] = $address['last_name'];
	
	// $name == billing  - 账单地址
	// $name == shipping - 送货地址
	// 送货地址没有 email 和 phone 这两项
	if( $name == 'billing' ){
		$new_address['email'] = get_user_meta( $customer_id, $name . '_email', true );
		$new_address['phone'] = get_user_meta( $customer_id, $name . '_phone', true );
	}
	
	return $new_address;
}

// 告诉 wc 将 {email} 和 {phone} 替换成实际的值
add_filter( 'WooCommerce_formatted_address_replacements', 'wc_my_account_address_replacements', 10, 2 );
function wc_my_account_address_replacements( $replacements, $args ){
	$replacements['{email}'] = $args['email'];
	$replacements['{phone}'] = $args['phone'];
	return $replacements;
}

my account 页面地址部分的模版是 templates/myaccount/my-address.php

备注

第一段代码中的格式会根据国家不同而不同,使用时应弄清楚针对哪个国家的用户,WooCommerce 具体预设了哪些国家的地址格式,看下面源代码

public function get_address_formats() {

	if (!$this->address_formats) :

		// Common formats
		$postcode_before_city = "{company}
{name}
{address_1}
{address_2}
{postcode} {city}
{country}";

		// Define address formats
		$this->address_formats = apply_filters('WooCommerce_localisation_address_formats', array(
			'default' => "{name}
{company}
{address_1}
{address_2}
{city}
{state}
{postcode}
{country}",
			'AU' => "{name}
{company}
{address_1}
{address_2}
{city} {state} {postcode}
{country}",
			'AT' => $postcode_before_city,
			'BE' => $postcode_before_city,
			'CA' => "{company}
{name}
{address_1}
{address_2}
{city} {state} {postcode}
{country}",
			'CH' => $postcode_before_city,
			'CN' => "{country} {postcode}
{state}, {city}, {address_2}, {address_1}
{company}
{name}",
			'CZ' => $postcode_before_city,
			'DE' => $postcode_before_city,
			'EE' => $postcode_before_city,
			'FI' => $postcode_before_city,
			'DK' => $postcode_before_city,
			'FR' => "{company}
{name}
{address_1}
{address_2}
{postcode} {city_upper}
{country}",
			'HK' => "{company}
{first_name} {last_name_upper}
{address_1}
{address_2}
{city_upper}
{state_upper}
{country}",
			'HU' => "{name}
{company}
{city}
{address_1}
{address_2}
{postcode}
{country}",
			'IS' => $postcode_before_city,
			'IT' => "{company}
{name}
{address_1}
{address_2}
{postcode}
{city}
{state_upper}
{country}",
			'JP' => "{postcode}
{state}{city}{address_2}
{address_1}
{company}
{last_name} {first_name}
 {country}",
			'LI' => $postcode_before_city,
			'NL' => $postcode_before_city,
			'NZ' => "{name}
{company}
{address_1}
{address_2}
{city} {postcode}
{country}",
			'NO' => $postcode_before_city,
			'PL' => $postcode_before_city,
			'SK' => $postcode_before_city,
			'SI' => $postcode_before_city,
			'ES' => "{name}
{company}
{address_1}
{address_2}
{postcode} {city}
{state}
{country}",
			'SE' => $postcode_before_city,
			'TR' => "{name}
{company}
{address_1}
{address_2}
{postcode} {city} {state}
{country}",
			'US' => "{name}
{company}
{address_1}
{address_2}
{city}, {state} {postcode}
{country}",
			'VN' => "{name}
{company}
{address_1}
{city}
{country}",
		));
	endif;

	return $this->address_formats;
}

如果要强制所有国家使用相同格式,则第一段代码需要改为

add_filter( 'WooCommerce_localisation_address_formats', 'wc_my_account_address_formats' );
function wc_my_account_address_formats( $formats ){
	$new_formats['default'] = "{name}
{email}
{phone}";
	return $new_formats; 
}