在Woocommerce 3中自定义我的帐户地址字段 [英] Customizing my-account addresses fields in Woocommerce 3

查看:142
本文介绍了在Woocommerce 3中自定义我的帐户地址字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在模板form-edit-addresses.php中删除表单字段billing_adress_2.

I would like to remove the form field billing_adress_2 within the template form-edit-addresses.php.

最后,是否可以像这样对表单字段重新排序:

To finish is it possible to reorder the form fields likes this:

名字-姓氏

电子邮件-电话

公司

地址_1

邮政编码

城市

状态

此更改我只想将其应用于form-edit-addresses.php页面(模板)

This change I would like to apply it only on the page (templates) form-edit-addresses.php

感谢您的帮助

推荐答案

在我的帐户">地址"部分中,以下挂钩的功能将:

In My account > Adresses section, the below hooked functions will:

  • 删除地址2"的结算和运送字段
  • 重新订购帐单和送货地址字段
  • 为帐单的电子邮件和电话字段重新排序(在名字和姓氏之后)
  • 从显示中删除地址2"

您忘记了可以在$ sorted_fields数组中轻松重新排序的国家/地区" 字段...

You forgot the "country" field that you can reorder easily in the $sorted_fields array…

代码:

// 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文件中.经过测试并可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and work.

如果您也想在结帐页面上使其有效,则必须删除以下行:

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;

在每个函数中(2次) ...

In each function (2 times)

这篇关于在Woocommerce 3中自定义我的帐户地址字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆