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

时间:2019-08-30 15:50:57

标签: php wordpress woocommerce checkout hook-woocommerce

我正在使用woocommerce_checkout_fields过滤器来编辑woocommerce字段标签的值。它在结帐页面上运行良好(如您所料),但是我不明白为什么它在帐户页面上也无效。我以为这些领域还是取材于同一地方?更具体地说,我说的是woocommerce帐户页面上edit-address端点上的地址字段?

我的代码尝试:

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

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

1 个答案:

答案 0 :(得分:1)

  

挂钩woocommerce_checkout_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