gpt4 book ai didi

php - 自定义 WooCommerce 我的帐户和结账上的地址字段

转载 作者:行者123 更新时间:2023-12-05 03:58:47 24 4
gpt4 key购买 nike

我正在使用 woocommerce_checkout_fields 过滤器来编辑 woocommerce 字段标签的值。它在结帐页面上运行良好(如您所料),但我不明白为什么它在帐户页面上也不起作用。我以为这些字段仍然来自同一个地方?更具体地说,我在谈论 woocommerce 帐户页面上编辑地址端点上的地址字段?

我的代码尝试:

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' );

“我的帐户”>“地址”(编辑帐单或送货地址)中的字段未自定义。

最佳答案

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

以下内容将同时影响我的帐户“地址” 部分字段和结帐字段,允许在相关的我的帐户部分自定义计费和送货字段。


1) 对于“我的帐户”和“结帐”中的地址字段(计费和送货):

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

// 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() 来定位我的帐户页面或结帐页面......


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

// 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) 对于我的帐户编辑地址和结帐上的运输字段

// 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) 所有(其他)字段仅在结帐时:

// 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) 此外,根据所选国家/地区,您应该需要使用过滤器:

  • woocommerce_country_locale_field_selectors
  • woocommerce_get_country_locale_default

那些位于 WC_Country 类。

代码进入事件子主题(或事件主题)的 functions.php 文件。


相关官方文档:Customizing checkout fields using actions and filters

关于php - 自定义 WooCommerce 我的帐户和结账上的地址字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57729859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com