在 WooCommerce My account 和 Checkout 上自定义地址字段 [英] Customize addresses fields on WooCommerce My account and Checkout

查看:19
本文介绍了在 WooCommerce My account 和 Checkout 上自定义地址字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 woocommerce_checkout_fields 过滤器来编辑 woocommerce 字段标签的值.它在结帐页面上运行良好(如您所料),但是我不明白为什么它不在帐户页面上生效.我以为这些领域仍然是从同一个地方形成的?更具体地说,我说的是 woocommerce 帐户页面上编辑地址端点上的地址字段?

I'm using the woocommerce_checkout_fields filter to edit the value of woocommerce field labels. It works fine on the checkout page (as you might expect), however I cannot understand why it doesn't also take effect on the account pages. I thought these fields were still taken form the same place? More specifically, I'm talking about the address fields on the edit-address endpoint on woocommerce account pages?

我的代码尝试:

function custom_woocommerce_fields( $fields ) {

    // Billing Fields
    $fields['billing']['billing_first_name']['label'] = 'First name';
    $fields['billing']['billing_last_name']['label'] = 'Last name';
    $fields['billing']['billing_company']['label'] = 'Company name';
    $fields['billing']['billing_address_1']['label'] = 'Street address';
    $fields['billing']['billing_address_2']['label'] = 'Apartment, unit, etc.';
    $fields['billing']['billing_city']['label'] = 'City';
    $fields['billing']['billing_country']['label'] = 'Country';
    $fields['billing']['billing_state']['label'] = 'County/State';
    $fields['billing']['billing_postcode']['label'] = 'Postcode';
    $fields['billing']['billing_email']['label'] = 'Email';
    $fields['billing']['billing_phone']['label'] = 'Phone';

    // Shipping Fields
    $fields['shipping']['shipping_first_name']['label'] = 'First name';
    $fields['shipping']['shipping_last_name']['label'] = 'Last name';
    $fields['shipping']['shipping_company']['label'] = 'Company name';
    $fields['shipping']['shipping_address_1']['label'] = 'Street address';
    $fields['shipping']['shipping_address_2']['label'] = 'Apartment, unit, etc.';
    $fields['shipping']['shipping_city']['label'] = 'City';
    $fields['shipping']['shipping_country']['label'] = 'Country';
    $fields['shipping']['shipping_state']['label'] = 'County/State';
    $fields['shipping']['shipping_postcode']['label'] = 'Postcode';
    $fields['shipping']['shipping_email']['label'] = 'Email';
    $fields['shipping']['shipping_phone']['label'] = 'Phone';

    // Account Fields
    $fields['account']['account_username']['label'] = 'Username or email';
    $fields['account']['account_password']['label'] = 'Password';

    // Order Fields
    $fields['order']['order_comments']['label'] = 'Order notes';

    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_woocommerce_fields' );

未在我的帐户 > 地址(编辑帐单或送货地址)上自定义字段.

Fields doesn't get customized on My account > Addresses (edit billing or shipping address).

推荐答案

钩子 woocommerce_checkout_fields 只允许在结帐页面上进行自定义,并且不会影响我的帐户地址"部分字段.

The hook woocommerce_checkout_fields only allow customizations on checkout page and will not affect the My account "Addresses" section fields.

以下内容将影响我的帐户地址"部分字段和结帐字段,允许在相关的我的帐户部分也自定义账单和运输字段.

The following will affect both My account "Addresses" section fields and checkout fields, allowing to make billing and shipping fields customized also on the related my account section.

1) 对于我的帐户"和结帐"上的地址字段(账单和运费):

1) For addresses fields (both billing and shipping) on My account and checkout:

在某些情况下,您需要对地址字段使用此过滤器,它会应用于所有账单和送货默认字段:

In some cases you need to use this filter for addresses fields and it's applied to all billing and shipping default fields:

// Billing and Shipping fields on my account edit-addresses and checkout
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $fields ) {
    $fields['first_name']['label'] = 'First name';
    $fields['last_name']['label'] = 'Last name';
    $fields['company']['label'] = 'Company name';
    $fields['address_1']['label'] = 'Street address';
    $fields['address_2']['label'] = 'Apartment, unit, etc.';
    $fields['city']['label'] = 'City';
    $fields['country']['label'] = 'Country';
    $fields['state']['label'] = 'County/State';
    $fields['postcode']['label'] = 'Postcode';

    return $fields;
}

您可以使用 WooCommerce 条件标签 is_account_page() 和 is_checkout() 来定位我的帐户页面或结帐页面...

You can use WooCommerce conditional tags is_account_page() and is_checkout() to target my account pages or checkout page…

2) 对于我的帐户编辑地址和结帐上的帐单字段:

2) For Billing fields on my account edit-addresses and checkout:

// Billing fields on my account edit-addresses and checkout
add_filter( 'woocommerce_billing_fields' , 'custom_billing_fields' );
function custom_billing_fields( $fields ) {

    // Billing Fields
    $fields['billing_first_name']['label'] = 'First name';
    $fields['billing_last_name']['label'] = 'Last name';
    $fields['billing_company']['label'] = 'Company name';
    $fields['billing_address_1']['label'] = 'Street address';
    $fields['billing_address_2']['label'] = 'Apartment, unit, etc.';
    $fields['billing_city']['label'] = 'City';
    $fields['billing_country']['label'] = 'Country';
    $fields['billing_state']['label'] = 'County/State';
    $fields['billing_postcode']['label'] = 'Postcode';
    $fields['billing_email']['label'] = 'Email';
    $fields['billing_phone']['label'] = 'Phone';

    return $fields;
}

<小时>

3) 对于我的帐户编辑地址和结帐中的送货字段


3) For Shipping fields on my account edit-addresses and checkout

// Shipping fields on my account edit-addresses and checkout
add_filter( 'woocommerce_shipping_fields' , 'custom_shipping_fields' );
function custom_shipping_fields( $fields ) {

    // Shipping Fields
    $fields['shipping_first_name']['label'] = 'First name';
    $fields['shipping_last_name']['label'] = 'Last name';
    $fields['shipping_company']['label'] = 'Company name';
    $fields['shipping_address_1']['label'] = 'Street address';
    $fields['shipping_address_2']['label'] = 'Apartment, unit, etc.';
    $fields['shipping_city']['label'] = 'City';
    $fields['shipping_country']['label'] = 'Country';
    $fields['shipping_state']['label'] = 'County/State';
    $fields['shipping_postcode']['label'] = 'Postcode';
    $fields['shipping_email']['label'] = 'Email';
    $fields['shipping_phone']['label'] = 'Phone';

    return $fields;
}

<小时>

4) 仅在结帐时所有(其他)字段:


4) All (other) fields only on checkout:

// All fields only on checkout
add_filter( 'woocommerce_checkout_fields' , 'other_custom_checkout_fields' );
function other_custom_checkout_fields( $fields ) {

    // Account Fields
    $fields['account']['account_username']['label'] = 'Username or email';
    $fields['account']['account_password']['label'] = 'Password';

    // Order Fields
    $fields['order']['order_comments']['label'] = 'Order notes';

    return $fields;
}

<小时>

5) 同样取决于所选国家/地区,您应该需要使用过滤器:


5) Also depending on the selected country, you should need to use the filters:

  • woocommerce_country_locale_field_selectors
  • woocommerce_get_country_locale_default

那些位于 WC_Country 类.

代码位于活动子主题(或活动主题)的 functions.php 文件中.

Code goes in functions.php file of your active child theme (or active theme).

相关官方文档:自定义结账使用操作和过滤器的字段

这篇关于在 WooCommerce My account 和 Checkout 上自定义地址字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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