admin管理员组

文章数量:1296844

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I am trying to populate a custom field called Customer Type in checkout with the current user role. I tried the below in my functions.php of my child theme and thought it would work but it does nothing. Can anyone tell me what I might be doing wrong?

$user = wp_get_current_user();
function onboarding_update_fields( $fields = array() ) {

   $fields['customertype'] = $user;

   return $fields;
}

add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I am trying to populate a custom field called Customer Type in checkout with the current user role. I tried the below in my functions.php of my child theme and thought it would work but it does nothing. Can anyone tell me what I might be doing wrong?

$user = wp_get_current_user();
function onboarding_update_fields( $fields = array() ) {

   $fields['customertype'] = $user;

   return $fields;
}

add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );
Share Improve this question edited Apr 13, 2021 at 19:38 Antti Koskinen 5,9838 gold badges15 silver badges26 bronze badges asked Apr 11, 2021 at 23:38 JayJay 1 1
  • WooCommerce and other 3rd party plugins are off topic and not in this stacks scope. You should ask via their official support routes or in their communities. – Tom J Nowell Commented Apr 14, 2021 at 10:15
Add a comment  | 

1 Answer 1

Reset to default 1

wp_get_current_user() returns a user object. You can't output objects directly.

Proceed by getting the users roles like $roles = (array)$user->roles;. Note that this returns an array though, so you will probably want to use the first element with $roles[0].

Your code might look like this (untested):

function onboarding_update_fields( $fields = array() ) {
   $user = wp_get_current_user();
   $roles = (array)$user->roles;
   $fields['customertype'] = $roles[0];
   return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );

Additionaly, you might want to check if the user is logged in, otherwise there might not be a user and role available.

WordPress Code Reference articles: wp_get_current_user, User object.

本文标签: phpHow do I populate custom field with current user role in Woocommerce