结帐字段 - 将billing_address_2设置在billing_address_1上方

时间:2017-02-06 12:08:50

标签: php wordpress woocommerce field checkout

在WooCommerce结帐字段中,我尝试在结帐表单上的billing_address_1上方设置billing_address_2。

所以不是:

Street Address
Apartment

我想:

Apartment
Street Address

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

我怎样才能做到这一点?

感谢。

1 个答案:

答案 0 :(得分:3)

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

这里有一个删除"地址"来自Address1字段的文本标签并将其设置为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;
}

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

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文件中,或者放在任何插件文件中。

代码经过测试并有效。

相关答案:Checkout fields: Hiding and showing existing fields

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