结帐字段 - 使 billing_address_2 高于 billing_address_1

Checkout fields - Make billing_address_2 above billing_address_1

在 WooCommerce 结帐字段中,我试图在结帐表单上 billing_address_2 高于 billing_address_1。

所以不是:

Street Address
Apartment

我想要:

Apartment
Street Address

另请注意,我正在使用名为 Avada 的主题。

我怎样才能做到这一点?

谢谢。

更新(与您的评论相关)…

这里你有一个添加,从 Address1 字段中删除 "Address" 文本标签并将其设置为 Address2 字段,使该字段(可选)成为必填字段并稍微更改占位符......我还有另一个解决方案(见代码后)。

代码如下:

add_filter( 'woocommerce_checkout_fields', 'custom_billing_fields_order' );
function custom_billing_fields_order( $fields ) {

    // 1. Customizing address_1 and address_2 fields
    $fields['billing']['billing_address_1']['label'] = ''; // Removing the label from Adress1
    $fields['billing']['billing_address_2']['label'] = __('Address', 'theme_domain');
    $fields['billing']['billing_address_2']['required'] = true; // Making Address 2 field required
    $fields['billing']['billing_address_2']['placeholder'] = __('Apartment, suite, unit etc...', 'woocommerce');

    // 2. Custom ordering the billing fields array (toggling address_1 with address_2)
    $custom_fields_order = array(
        'billing_first_name', 'billing_last_name',
        'billing_company',
        'billing_email',      'billing_phone',
        'billing_country',
        'billing_address_2',  'billing_address_1',  ##  <== HERE, changed order
        'billing_postcode',   'billing_city'
    );

    foreach($custom_fields_order as $field)
        $new_ordered_fields[$field] = $fields['billing'][$field];

    // Replacing original fields order by the custom one
    $fields['billing'] = $new_ordered_fields;


    // Returning Checkout customized billing fields order
    return $fields;
}

您可以反转字段占位符并添加(可选)需要字段 Address2,而不是切换这 2 个字段,这样您就不需要对字段重新排序。你可以这样做:

add_filter( 'woocommerce_checkout_fields', 'custom_billing_fields_placeholders' );
function custom_billing_fields_placeholders( $fields ) {

    // 1. Customizing address_1 and address_2 fields
    $fields['billing']['billing_address_1']['placeholder'] = __('Apartment, suite, unit etc...', 'woocommerce');
    $fields['billing']['billing_address_2']['required'] = true; // Making Address 2 field required
    $fields['billing']['billing_address_2']['placeholder'] = __('Street address', 'woocommerce');

    // Returning Checkout customized billing fields
    return $fields;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

代码已经过测试并且可以工作。


相关回答:

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