修改 WooCommerce My Account 页面的地址格式
优质
小牛编辑
148浏览
2023-12-01
WooCommerce My Account页面默认地址格式是姓名、公司、地址1、地址2、城市、省/州、邮编和国家,更改这个格式需要先更新格式字符串,然后获取所有需要替换的数据,具体方法如下所示。
例如,只显示姓名、电子邮箱和电话,代码放在主题的functions.php里。Woocommerce 2.1.9下测试。
// 更新地址格式字符串 // 默认是{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country} add_filter( 'woocommerce_localisation_address_formats', 'wc_my_account_address_formats' ); function wc_my_account_address_formats( $formats ){ $formats['default'] = "{name}\n{email}\n{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}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}"; // Define address formats $this->address_formats = apply_filters('woocommerce_localisation_address_formats', array( 'default' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}", 'AU' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}", 'AT' => $postcode_before_city, 'BE' => $postcode_before_city, 'CA' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}", 'CH' => $postcode_before_city, 'CN' => "{country} {postcode}\n{state}, {city}, {address_2}, {address_1}\n{company}\n{name}", 'CZ' => $postcode_before_city, 'DE' => $postcode_before_city, 'EE' => $postcode_before_city, 'FI' => $postcode_before_city, 'DK' => $postcode_before_city, 'FR' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city_upper}\n{country}", 'HK' => "{company}\n{first_name} {last_name_upper}\n{address_1}\n{address_2}\n{city_upper}\n{state_upper}\n{country}", 'HU' => "{name}\n{company}\n{city}\n{address_1}\n{address_2}\n{postcode}\n{country}", 'IS' => $postcode_before_city, 'IT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode}\n{city}\n{state_upper}\n{country}", 'JP' => "{postcode}\n{state}{city}{address_2}\n{address_1}\n{company}\n{last_name} {first_name}\n {country}", 'LI' => $postcode_before_city, 'NL' => $postcode_before_city, 'NZ' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {postcode}\n{country}", 'NO' => $postcode_before_city, 'PL' => $postcode_before_city, 'SK' => $postcode_before_city, 'SI' => $postcode_before_city, 'ES' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}", 'SE' => $postcode_before_city, 'TR' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city} {state}\n{country}", 'US' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}, {state} {postcode}\n{country}", 'VN' => "{name}\n{company}\n{address_1}\n{city}\n{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}\n{email}\n{phone}"; return $new_formats; }