在“My account > 地址”部分,以下挂钩功能将会:
- 移除“地址2”账单和发货字段
- 重新排序账单和发货地址字段
- 在名字之后重新排序账单电子邮件和电话字段
- 从显示中移除“地址2”
你忘记了可以在$sorted_fields数组中轻松重新排序的“国家”字段…
代码:
add_filter( 'woocommerce_default_address_fields', 'custom_default_address_fields', 20, 1 );
function custom_default_address_fields( $fields ) {
if( ! is_account_page() ) return $fields;
unset($fields['address_2']);
$sorted_fields = array('first_name','last_name','company','address_1','country','postcode','city','state');
$new_fields = array();
$priority = 0;
foreach($sorted_fields as $key_field){
$priority += 10;
if( $key_field == 'company' )
$priority += 20;
$new_fields[$key_field] = $fields[$key_field];
$new_fields[$key_field]['priority'] = $priority;
}
return $new_fields;
}
add_filter( 'woocommerce_billing_fields', 'custom_billing_fields', 20, 1 );
function custom_billing_fields( $fields ) {
if( ! is_account_page() ) return $fields;
$fields['billing_email']['priority'] = 30;
$fields['billing_email']['class'] = array('form-row-first');
$fields['billing_phone']['priority'] = 40;
$fields['billing_phone']['class'] = array('form-row-last');
return $fields;
}
add_filter( 'woocommerce_my_account_my_address_formatted_address' , 'my_account_address_formatted_addresses', 20, 3 );
function my_account_address_formatted_addresses( $address, $customer_id, $address_type ) {
unset($address['address_2']);
return $address;
}
您需要将代码放入您的活动子主题(或活动主题)的function.php文件中。已经经过测试并且工作正常。
If you want to make that effective in checkout page too, you will have to remove this lines:
// Only on account pages
if( ! is_account_page() ) return $fields;
In each function (2 times)…
![enter image description here](https://istack.dev59.com/flPau.webp)
$sorted_fields
中的项目进行排序...排序是通过属性"priority"完成的... - LoicTheAztec