WooCommerce默认占位符在结账表单中没有消失

3
我正在使用 woocommerce_checkout_fields 来更改标签和字段。以下是我的代码。
function custom_override_checkout_fields($fields) {


     $fields['billing']['billing_first_name'] = array(
            'label' => '',
            'placeholder' => _x('First Name*', 'placeholder', 'woocommerce'),
            'required' => true,
            'class' => array('checkout-billing-first-name')
        );
        $fields['billing']['billing_last_name'] = array(
            'label' => '',
            'placeholder' => _x('last Name*', 'placeholder', 'woocommerce'),
            'required' => true,
            'class' => array('checkout-billing-last-name')
        );
        $fields['billing']['billing_company'] = array(
            'label' => '',
            'placeholder' => _x('Company Name', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('checkout-billing-company')
        );
        $fields['billing']['billing_address_1'] = array(
            'label' => '',
            'placeholder' => _x('Address(Line 1)*', 'placeholder', 'woocommerce'),
            'required' => true,
            'class' => array('checkout-billing-addressL1')
        );
         $fields['billing']['billing_address_2'] = array(
            'label' => '',
            'placeholder' => _x('Address(Line 2)*', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('checkout-billing-addressL2')
        );

        return $fields;
    }

    add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');

一切都正常,但是账单地址1和账单地址2的占位符只有在加载时才会改变,即在页面加载后只显示默认的占位符。


你的代码在我的端上运行得非常好。 - Anand Shah
如果它在页面加载后显示,那么它可能是通过JavaScript动态更改的。 - rnevius
我有同样的问题。有什么想法吗? - dominotrix
3个回答

2

尝试使用过滤器woocommerce_default_address_fields。

例如:

function pawelprotas_wc_default_address_fields($fields){
    $fields['address_1']['placeholder'] = __('Street address *', 'loft');
    $fields['address_2']['placeholder'] = __('Apartment, suite, unit etc.', 'loft');

    return $fields;
}

add_filter('woocommerce_default_address_fields', 'pawelprotas_wc_default_address_fields');

1

首先添加以下过滤器以防止JS替换占位符。

add_filter( 'woocommerce_country_locale_field_selectors' , 'override_billing_checkout_fields', 10, 1 );

function override_billing_checkout_fields( $locale_fields ) {
    $locale_fields = array(
        'address_1' => '',
        'address_2' => ''
    );
    return $locale_fields;
}

然后使用以下过滤器

add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields)
{
    $fields['billing']['billing_address_1']['placeholder'] = __( 'Address 1', 'woocommerce' );
    return $fields;
}

0

您也可以更改默认的占位符:

add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields)
{
$fields['billing']['billing_company']['placeholder'] = 'Business Name';
return $fields;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接