Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1296844
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 questionI 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 questionI 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
1 Answer
Reset to default 1wp_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
版权声明:本文标题:php - How do I populate custom field with current user role in Woocommerce 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741618649a2388676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论