所有WooCommerce结帐字段的自定义占位符 [英] Custom placeholder for all WooCommerce checkout fields
问题描述
我正试图在WooCommerce结帐字段中添加一个占位符,并且该字段对于除电话和电子邮件字段以外的所有字段均适用。
I am trying to add a placeholder to my WooCommerce checkout fields, and it's working perfectly for every field except for the phone and the email fields.
这是我正在使用的代码:
This is the code I am using:
add_filter('woocommerce_default_address_fields', 'override_address_fields');
function override_address_fields( $address_fields ) {
$address_fields['first_name']['placeholder'] = 'Fornavn';
$address_fields['last_name']['placeholder'] = 'Efternavn';
$address_fields['address_1']['placeholder'] = 'Adresse';
$address_fields['state']['placeholder'] = 'Stat';
$address_fields['postcode']['placeholder'] = 'Postnummer';
$address_fields['city']['placeholder'] = 'By';
$address_fields['phone']['placeholder'] = 'Telefon';
$address_fields['email']['placeholder'] = 'Email';
return $address_fields;
}
我要去哪里了?为什么电话和电子邮件没有显示任何结果?
Where am I going wrong? Why isn't the phone and email putting out any results?
我已经使用浏览器开发人员工具检查器从这两个字段中获取了ID。
I have taken the IDs from that two fields using my browser developer tool inspector.
编辑:
我也尝试了以下建议代码:
add_filter('woocommerce_checkout_fields', 'override_checkout_fields');
function override_checkout_fields( $checkout_fields ) {
$checkout_fields['first_name']['placeholder'] = 'Fornavn';
$checkout_fields['last_name']['placeholder'] = 'Efternavn';
$checkout_fields['address_1']['placeholder'] = 'Adresse';
$checkout_fields['state']['placeholder'] = 'Stat';
$checkout_fields['postcode']['placeholder'] = 'Postnummer';
$checkout_fields['city']['placeholder'] = 'By';
$checkout_fields['phone']['placeholder'] = 'Telefon';
$checkout_fields['email']['placeholder'] = 'Email';
return $checkout_fields;
}
这也是:
add_filter('woocommerce_checkout_fields', 'override_checkout_fields');
function override_checkout_fields( $checkout_fields ) {
$checkout_fields['billing_first_name']['placeholder'] = 'Fornavn';
$checkout_fields['billing_last_name']['placeholder'] = 'Efternavn';
$checkout_fields['billing_address_1']['placeholder'] = 'Adresse';
$checkout_fields['billing_state']['placeholder'] = 'Stat';
$checkout_fields['billing_postcode']['placeholder'] = 'Postnummer';
$checkout_fields['billing_city']['placeholder'] = 'By';
$checkout_fields['billing_phone']['placeholder'] = 'Telefon';
$checkout_fields['billing_email']['placeholder'] = 'Email';
return $checkout_fields;
}
但这不起作用。
推荐答案
查看官方文档,您会看到没有 电话
和<$使用 woocommerce_default_address_fields
过滤器挂钩时,默认地址的c $ c>'电子邮件'关键字段为默认地址。
可接受的字段键为:
国家
,名字
,姓氏
,公司
,地址_1
,地址_2
,城市
,州
,邮政编码
。
Looking at the official docs, you will see that there is no 'phone'
and 'email'
key fields for the default addresses when using woocommerce_default_address_fields
filter hook.
Accepted fields keys are:
country
, first_name
, last_name
, company
, address_1
, address_2
, city
, state
, postcode
.
这就是为什么您可以使用 woocommerce_default_address_fields
This is why you can get changes using woocommerce_default_address_fields
电子邮件和电话是 billing 字段,可通过 woocommerce_checkout_fields
过滤器挂钩使用。它们被命名(请参阅文档)'billing_phone'
和'billing_phone'
…
Email and phone are billing fields and they are available trough woocommerce_checkout_fields
filter hook. They are named (see in the documentation) 'billing_phone'
and 'billing_phone'
…
覆盖它们的正确方法是:
The correct way to override them is:
add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
$fields['billing']['billing_phone']['placeholder'] = 'Telefon';
$fields['billing']['billing_email']['placeholder'] = 'Email';
return $fields;
}
其他字段(计费和运输):
And for the others fields (billing and shipping):
add_filter('woocommerce_default_address_fields', 'override_default_address_checkout_fields', 20, 1);
function override_default_address_checkout_fields( $address_fields ) {
$address_fields['first_name']['placeholder'] = 'Fornavn';
$address_fields['last_name']['placeholder'] = 'Efternavn';
$address_fields['address_1']['placeholder'] = 'Adresse';
$address_fields['state']['placeholder'] = 'Stat';
$address_fields['postcode']['placeholder'] = 'Postnummer';
$address_fields['city']['placeholder'] = 'By';
return $address_fields;
}
代码位于您的活动子主题(或主题)的function.php文件中,或者
所有代码都在Woocommerce 3+上进行了测试,并且可以正常工作。
All code is tested on Woocommerce 3+ and works.
参考:使用操作和过滤器
用于登录表单字段:自定义WooCommerce登录表单用户字段
这篇关于所有WooCommerce结帐字段的自定义占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!