自訂 WooCommerce 我的帳戶和結帳上的地址字段
P粉463824410
P粉463824410 2023-10-29 19:34:04
0
1
874

我正在使用 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' );

「我的帳戶」>「地址」中的欄位未進行自訂(編輯帳單或送貨地址)。

P粉463824410
P粉463824410

全部回覆(1)
P粉714890053

以下內容將影響我的帳戶「地址」部分字段和結帳字段,從而允許在相關的我的帳戶部分上自訂帳單和送貨字段。


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 檔案中。


相關官方文件:自訂結帳使用操作和過濾器的欄位

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板