我已经实现了@zipkundan建议的解决方案,如下所示:
add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
$current_user = wp_get_current_user();;
$user_id = $current_user->ID;
foreach ( $checkout_fields['billing'] as $key => $field ){
if($key == 'billing_address_1' || $key == 'billing_address_2' || $key == 'billing_first_name' || $key == 'billing_check_business' || $key == 'billing_company_name' || $key == 'billing_last_name' || $key == 'billing_codice_fiscale' || $key == 'billing_vat' || $key == 'billing_address_1' || $key == 'billing_city' || $key == 'billing_state' || $key == 'billing_postcode' || $key == 'billing_phone' || $key == 'billing_email'){
$key_value = get_user_meta($user_id, $key, true);
if( strlen($key_value)>0){
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
,除了billing_state和billing_check_business之外,它都能正常工作。 billing_state是一个选择字段billing_check_business是一个rado字段。关于如何使此代码也适用于select和radio的任何建议?谢谢
答案 0 :(得分:0)
add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields');
function my_woocommerce_billing_fields($fields)
{
$fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_address_1']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_address_2']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_postcode']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_city']['custom_attributes'] = array('readonly'=>'readonly');
return $fields;
}