Passen Sie Adressfelder in „Mein Konto' und „Kasse' von WooCommerce an
P粉463824410
P粉463824410 2023-10-29 19:34:04
0
1
917

Ich verwende den woocommerce_checkout_fields-Filter, um den Wert der Woocommerce-Feldbezeichnung zu bearbeiten. Auf der Checkout-Seite funktioniert es einwandfrei (wie erwartet), aber ich kann nicht verstehen, warum es auf der Kontoseite nicht funktioniert. Ich dachte, diese Felder stammen immer noch vom selben Ort? Genauer gesagt spreche ich vom Adressfeld am Endpunkt „Adresse bearbeiten“ auf der Woocommerce-Kontoseite?

Mein Codeversuch:

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

Felder in „Mein Konto“ >Adressen werden nicht angepasst (Bearbeitung von Rechnungs- oder Lieferadressen).

P粉463824410
P粉463824410

Antworte allen(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 文件中。


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

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage