我正在尝试在我的WooCommerce结帐字段中添加占位符,并且除了电话和电子邮件字段外,它对每个字段都非常有效。
这是我正在使用的代码:
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;
}
我哪里错了?为什么电话和电子邮件没有发布任何结果?
我使用浏览器开发人员工具检查员从这两个字段中获取了ID。
修改
我也尝试了这个建议的代码:
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;
}
但它不起作用。
答案 0 :(得分:6)
查看official docs,当您使用 {{时,您会看到没有 'phone'
和'email'
个关键字段作为默认地址1}} 过滤钩子。
接受的字段键是:
woocommerce_default_address_fields
,country
,first_name
,last_name
,company
,address_1
,address_2
,city
,{{ 1}}。
这就是您可以使用 state
电子邮件和电话是结算字段,可通过 postcode
过滤器钩子获取。它们的名称(参见文档)woocommerce_default_address_fields
和woocommerce_checkout_fields
...
覆盖它们的正确方法是:
'billing_phone'
对于其他字段(结算和发货):
'billing_phone'
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
所有代码都在Woocommerce 3+上进行测试并且有效。
答案 1 :(得分:0)
浏览WooCommerce documentation您可能想尝试使用woocommerce_checkout_fields
过滤器而不是woocommerce_default_address_fields
似乎更符合逻辑,因为上面的字段并非所有地址相关,而且该钩子派生自class-wc-countries.php
。
另请注意,字段名称根据页面的上下文而有所不同,例如,结算页面字段的名称如下:
希望有所帮助!祝你好运。
答案 2 :(得分:0)
根据官方文档https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ 这里有更新的代码:
add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = 'Prénom';
$fields['billing']['billing_last_name']['placeholder'] = 'Nom';
$fields['billing']['billing_company']['placeholder'] = 'Nom de la société (optionnel)';
$fields['billing']['billing_postcode']['placeholder'] = 'Code postal';
$fields['billing']['billing_phone']['placeholder'] = 'Téléphone';
$fields['billing']['billing_city']['placeholder'] = 'Ville';
$fields['shipping']['shipping_first_name']['placeholder'] = 'Prénom';
$fields['shipping']['shipping_last_name']['placeholder'] = 'Nom';
$fields['shipping']['shipping_company']['placeholder'] = 'Nom de la société (optionnel)';
$fields['shipping']['shipping_postcode']['placeholder'] = 'Code postal';
$fields['shipping']['shipping_phone']['placeholder'] = 'Téléphone';
$fields['shipping']['shipping_city']['placeholder'] = 'Ville';
return $fields;
}