问题
I've implemented the solution suggested by @zipkundan like this:
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;
}
and it works correctly except for billing_state and for billing_check_business. billing_state is a select field billing_check_business is a rado field. Any suggestion on how to make this code working also for select and radio? Thanks
回答1:
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;
}
来源:https://stackoverflow.com/questions/53541331/woocommerce-readonly-billing-fields-select-and-radio-fields