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; 
}

24条留言

  1. 妹纸,有什么办法能让注册的中文地址,姓名等信息自动变为拼音存在系统里呢?

    1. 中文转拼音你要找相应算法就能改,这个我没研究过

  2. 你好,这个好像对edit my address这个页面没有用啊,查看订单页面由于删除了一些地址,就会多出一些逗号,改动哪个模板会对所有的address都有用呢,

  3. 字段的顺序可以调整么?

    eg: 姓名
    地址
    邮箱

    变成
    邮箱
    姓名
    地址

    1. // 更新地址格式字符串
      // 默认是{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 ){
          $new_formats['default'] = "{email}\n{name}\n{company}\n{address_1}\n{address_2}\n{city}, {state} {postcode}\n{country}";
          return $new_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 ){
          // $name == billing  - 账单地址
          // $name == shipping - 送货地址
          // 送货地址没有email和phone这两项
          if( $name == 'billing' ){
              $address['email'] = get_user_meta( $customer_id, $name . '_email', true );
          }
          return $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'];
          return $replacements;
      }
      

      地址具体格式在第一个function里面调整

      1. 嘿 谢了.
        像add_filter( 'woocommerce_my_account_my_address_formatted_address', 'wc_my_account_custom_formatted_address', 10, 3); 中的函数名(?..咳咳,不太懂先这样叫吧)怎么找到的? 我是想修改 checkout 页面的顺序,或者是重构 checkout页面. 这段代码 修改的是 my_account 页面的显示顺序

  4. 我想把 地址2 去掉,修改了很多地方还是不成功,能帮忙说说大概改什么地方吗?

    1. 用woocommerce checkout manager插件可以实现。
      用代码的话,将下面的代码放到主题的functions.php里,这个会影响my account编辑地址的字段,还会影响checkout页面的字段,其实就是上面那个插件用的方法。

      add_filter( 'woocommerce_billing_fields', 'remove_billing_address_2');
      function remove_billing_address_2($address_fields){
      	unset($address_fields['billing_address_2']);
      	return $address_fields;
      }
      
      add_filter( 'woocommerce_shipping_fields', 'remove_shipping_address_2');
      function remove_shipping_address_2($address_fields){
      	unset($address_fields['shipping_address_2']);
      	return $address_fields;
      }
      
      1. 真是太感谢你了,谢谢!

      2. 其实想问 add_filter 怎么找里面需要调整的内容的啊

    1. 那我也试过,确实有反应,这问题可不是说句没反就能解决的,你需要知道如何调试代码。比如测试一下那些filter是否执行了,或者万一我写错了filter的名字,至少可以通过搜索检查filter是否真实存在。

  5. 解决的大问题~~~
    博主啊 还有一个问题 woocommerce文件里都会调用一个头文件“get_header( ‘shop’ );”可是我并没有定义这个头文件,甚至我的主题里都没有header.php它调用的哪里的头文件啊,我想修改都找不到地方

  6. 我想问一下,修改账单地址和配送地址的模板是哪个,谢谢!!

    1. 我想问一下,账户注册页面的地址顺序如何修改,改成顺序为 国家 省 市,详细地址…..

        1. 什么时间也上一篇修改注册页面地址顺序的教程啊,呵呵,网上扒遍了也没找到篇!!

          1. 注册页面有地址吗?你说的不是结账页面吗?

          2. 就是账户中心里面的那个填写地址的那个地方,填写的时候不是按顺序填写的。

            就是填写账单地址和收货地址哪里,不是按顺序来填写的,不符合中国人的输入习惯。

        2. 修改 woocommerce/templates/myaccount/my-address.php 这个文件没任何效果。

评论功能已关闭