在Woocommerce 3中自定义“我的账户地址”字段

3
我想在模板form-edit-addresses.php中删除表单字段billing_adress_2。
最后,是否可以按照以下顺序重新排列表单字段:
名字 - 姓氏
电子邮件 - 电话
公司
地址1
邮政编码
城市

我希望仅将此更改应用于页面(模板)form-edit-addresses.php。
感谢任何帮助。
2个回答

9
在“My account > 地址”部分,以下挂钩功能将会:
  • 移除“地址2”账单和发货字段
  • 重新排序账单和发货地址字段
  • 在名字之后重新排序账单电子邮件和电话字段
  • 从显示中移除“地址2”

你忘记了可以在$sorted_fields数组中轻松重新排序的“国家”字段…

代码:

// Account Edit Adresses: Remove and reorder addresses fields
add_filter(  'woocommerce_default_address_fields', 'custom_default_address_fields', 20, 1 );
function custom_default_address_fields( $fields ) {
    // Only on account pages
    if( ! is_account_page() ) return $fields;

    ## ---- 1.  Remove 'address_2' field ---- ##

    unset($fields['address_2']);

    ## ---- 2.  Sort Address fields ---- ##

    // Set the order (sorting fields) in the array below
    $sorted_fields = array('first_name','last_name','company','address_1','country','postcode','city','state');

    $new_fields = array();
    $priority = 0;

    // Reordering billing and shipping fields
    foreach($sorted_fields as $key_field){
        $priority += 10;

        if( $key_field == 'company' )
            $priority += 20; // keep space for email and phone fields

        $new_fields[$key_field] = $fields[$key_field];
        $new_fields[$key_field]['priority'] = $priority;
    }
    return $new_fields;
}

// Account Edit Adresses: Reorder billing email and phone fields
add_filter(  'woocommerce_billing_fields', 'custom_billing_fields', 20, 1 );
function custom_billing_fields( $fields ) {
    // Only on account pages
    if( ! is_account_page() ) return $fields;

    ## ---- 2.  Sort billing email and phone 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;
}

// Account Displayed Addresses : Remove 'address_2'
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']); // remove 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


1
@Dom 抱歉,我没有完全理解。有两个函数一起工作来进行排序...因为电子邮件和电话不是真正的地址字段,而是账户字段,所以它们在第二个函数中进行排序...然后对于自己的排序,就像你想要的那样,你可以轻松地对数组$sorted_fields中的项目进行排序...排序是通过属性"priority"完成的... - LoicTheAztec
这个函数一如既往地完美,现在我正在尝试修改它以适应我的需求,我会尽快告诉你......谢谢。 - Dom
太好了!非常感谢你。 - Dom

2

这段代码将按照您想要的顺序更改账单地址中字段的顺序。将此代码放置在您的主题的functions.php文件中。

add_filter('woocommerce_address_to_edit', 'reorder_woocommerce_address_fields', 10, 2);

function reorder_woocommerce_address_fields( $address, $load_address) {
    $new_address['billing_first_name'] = $address['billing_first_name'];
    $new_address['billing_last_name'] = $address['billing_last_name'];
    $new_address['billing_email'] = $address['billing_email'];
    $new_address['billing_phone'] = $address['billing_phone'];

    $new_address['billing_email'] = array(
        'label'     => __('Email', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-first'),
    );

    $new_address['billing_phone'] = array(
        'label'     => __('Phone', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-last'),
        'clear'     => true
    );

    $new_address['billing_company'] = $address['billing_company'];
    $new_address['billing_address_1'] = $address['billing_address_1'];
    $new_address['billing_postcode'] = $address['billing_postcode'];
    $new_address['billing_city'] = $address['billing_city'];
    $new_address['billing_state'] = $address['billing_state'];

    return $new_address;
}

好的!谢谢。我正在寻找保留空间字段类。 - Dom
这个函数很完美,正是我想要的。 非常感谢…… :) - Dom
1
@Dom 谢谢老兄。你能否投票支持我的答案并接受它,我将不胜感激 :) - Andrew Schultz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接