You are using get_currentuserinfo
in a wrong way because you are passing parameters that this functions doesn’t accept and you expect that the functions return directly what you want. get_currentuserinfo
doesn’t accept any parameter, returns a WP User object and require the global $current_user
var.
Anyway, I prefer to use wp_get_current_user()
in order to avoid working directly with globals:
$user_info = wp_get_current_user();
if ( ! $order_data['user_firstname'] ) {
if ( is_user_logged_in() ) {
$order_data['user_firstname'] = $user_info->user_firstname;
} else {
$order_data['user_firstname'] = 0;
}
}
// add user surname
if ( ! $order_data['user_surname'] ) {
if ( is_user_logged_in() ) {
$order_data['user_surname'] = $user_info->user_lastname;
} else {
$order_data['user_surname'] = 0;
}
}
And in your code, you are populating the array $order_data
with default values for each key. After that, you check, for example, if ( ! $order_data['user_surname'] )
; because $order_data['user_surname']
was filled with default values, all the code inside that if
statement won’t be executed. You should leave the code in this way (default values was already set, so it will modified only for logged in users):
if ( is_user_logged_in() ) {
$user_info = wp_get_current_user();
$order_data['user_firstname'] = $user_info->user_firstname;
// add user surname
$order_data['user_surname'] = $user_info->user_lastname;
}
You still say that the oly thing is not working is the name and last name. But you can verify yourself that it is working:
if ( is_user_logged_in() ) {
$user_info = wp_get_current_user();
$order_data['user_firstname'] = $user_info->user_firstname;
// add user surname
$order_data['user_surname'] = $user_info->user_lastname;
}
var_dump($order_data);
You will see how de dump data inludes the name and lastname if you are logged in. Please, debug the $order_data
.