问题描述
我想阻止(例如,使这些字段为只读的)用户在WooCommerce结帐表单上更改其帐单信息.
我当前正在使用以下代码段:
add_filter('woocommerce_billing_fields','mycustom_woocommerce_billing_fields',10,1);函数mycustom_woocommerce_billing_fields($ fields){$ fields ['billing_first_name'] ['custom_attributes'] = array('readonly'=>'readonly');$ fields ['billing_last_name'] ['custom_attributes'] = array('readonly'=>'readonly');$ fields ['billing_email'] ['custom_attributes'] = array('readonly'=>'readonly');$ fields ['billing_phone'] ['custom_attributes'] = array('readonly'=>'readonly');返回$ fields;}
但是问题是::如果用户未在注册中填写任何这些字段,则由于这些字段不可编辑,因此无法将其数据插入到结帐表格中.>
我的问题是:如果字段不为空,如何使它们成为只读(或禁用)
有人可以帮助我吗?
7uc1f3r的答案当然可以获取用户数据……但是自WooCommerce 3以来,您可以使用 WC_Checkout
get_value()
专用方法,如下所示:
add_filter('woocommerce_billing_fields','filter_wc_billing_fields',10,1);函数filter_wc_billing_fields($ fields){//在结帐时以及用户是否登录时if(is_checkout()&& is_user_logged_in()){//在下面定义您的关键字段$ keys_fields = ['billing_first_name','billing_last_name','billing_email','billing_phone'];//遍历您定义的特定字段foreach($ keys_fields as $ key){//检查当前字段是否存在值if(($ value = WC()-> checkout-> get_value($ key))&&!empty($ value)){//如果存在值则将其设为只读$ fields [$ key] ['custom_attributes'] = ['readonly'=>'readonly'];}}}返回$ fields;}
代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
如果您希望此代码在我的帐户">地址">编辑"中也处于活动状态,则只需从第一个 中删除
语句. is_checkout()&&
IF
I would like to prevent (make these fields readonly, for example) users from changing their billing information on the WooCommerce checkout form.
I'm currently using this code snippet:
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
function mycustom_woocommerce_billing_fields($fields)
{
$fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_email']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_phone']['custom_attributes'] = array('readonly'=>'readonly');
return $fields;
}
But the problem is: If the user has not filled in any of these fields in the registration, he is unable to insert his data in the checkout form because these fields are not editable.
My question is:If the fields are not empty, how to make them readonly (or disabled)
Someone who can help me with this?
The answer of 7uc1f3r certainly works getting the user data… But since WooCommerce 3, you can use WC_Checkout
get_value()
dedicated method as follow:
add_filter( 'woocommerce_billing_fields', 'filter_wc_billing_fields', 10, 1 );
function filter_wc_billing_fields( $fields ) {
// On checkout and if user is logged in
if ( is_checkout() && is_user_logged_in() ) {
// Define your key fields below
$keys_fields = ['billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone'];
// Loop through your specific defined fields
foreach ( $keys_fields as $key ) {
// Check that a value exist for the current field
if( ( $value = WC()->checkout->get_value($key) ) && ! empty( $value ) ) {
// Make it readonly if a value exist
$fields[$key]['custom_attributes'] = ['readonly'=>'readonly'];
}
}
}
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
If you want this code to be also active in My account > Addresses > edit…, you will just have to remove is_checkout() &&
from the first IF
statement.
这篇关于如何禁用WooCommerce结帐中预填写的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!