JayX Posted April 11, 2021 Share Posted April 11, 2021 (edited) I am trying to populate a custom field called "Customer Type" current user role. The custom field is displayed on my checkout page. 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' ); Edited April 11, 2021 by JayX Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/ Share on other sites More sharing options...
MadTechie Posted April 12, 2021 Share Posted April 12, 2021 (edited) I'm about to go to sleep, but for a quick first look, You'll need to have the $user inside the function. e.g. function onboarding_update_fields( $fields = array() ) { $user = wp_get_current_user(); $fields['customertype'] = $user; return $fields; } Hope this helps Edited April 12, 2021 by MadTechie Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585750 Share on other sites More sharing options...
JayX Posted April 12, 2021 Author Share Posted April 12, 2021 Thank you, I was hopeful that would do it because I know you are right but still does not work. Any other ideas? Thank you so much for responding Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585760 Share on other sites More sharing options...
MadTechie Posted April 12, 2021 Share Posted April 12, 2021 Can you pass the object? What happens when? Just trying assign a string function onboarding_update_fields( $fields = array() ) { $user = wp_get_current_user(); //$fields['customertype'] = $user; $fields['customer_firstname'] = $user->user_firstname; return $fields; } does customer_firstname work? You might need to assign what you need instead of the entire object Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585761 Share on other sites More sharing options...
JayX Posted April 12, 2021 Author Share Posted April 12, 2021 Unfortunately no, first name would not work for me. I sell products to end users, and through distributors, all of which order through the website. I use Shipstation plugin and I need to pass to shipstation whether it is a Customer, a Guest, or Distributor Tier1, Distributor Tier2, or Distributor Tier3 so that my automation in shipstation is correct. I have a custom field in the checkout that I created with the plugin Checkout Field Editor and I have successfully created code that makes that field pass to shipstation, now I just need to figure out how to automatically populate that field with the current user role. Just banging my head here, as it feels like I am so close. Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585766 Share on other sites More sharing options...
JayX Posted April 12, 2021 Author Share Posted April 12, 2021 Oh, and I did try the customer name snippet that you suggested just to see if it would pass that info, but it does not either. Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585767 Share on other sites More sharing options...
MadTechie Posted April 12, 2021 Share Posted April 12, 2021 Can you check the following? $fields['customer_firstname'] = "testing"; I know this isn't the end result i just want to check that you can receive the data passed Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585779 Share on other sites More sharing options...
JayX Posted April 13, 2021 Author Share Posted April 13, 2021 Nope, it passed no data. Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585805 Share on other sites More sharing options...
JayX Posted April 14, 2021 Author Share Posted April 14, 2021 Ok, I got it to pass data. Because I am using the Checkout Field Editor plugin I assume this is the reason I had to do it this way. Now I just need to figure out how to get the default to be the user role. Belo w is what I had to do to get it to even pass data. function custom_override_checkout_fields ( $fields ) { $fields['billing']['customer_type']['label'] = 'Customer Type'; $fields['billing']['customer_type']['default'] = 'one'; return $fields; } // End custom_override_checkout_fields() add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585821 Share on other sites More sharing options...
JayX Posted April 15, 2021 Author Share Posted April 15, 2021 For anyone that may want to know, the following snippet did the job. The only last thing I need to do is somehow make it show "Guest" if no role is found. I think I need an if else statement or something. Anyone have any ideas? function onboarding_update_fields( $fields = array() ) { $user = wp_get_current_user(); $roles = (array)$user->roles; $fields['billing']['customer_type']['default'] = $roles[0]; return $fields; } add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' ); Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585823 Share on other sites More sharing options...
gizmola Posted April 15, 2021 Share Posted April 15, 2021 I can't make any guarantee as to the ramifications of doing this, because you are working within the constraints of WooCommerce and Wordpress, but to solve your stated issue: function onboarding_update_fields( $fields = array() ) { $user = wp_get_current_user(); $roles = (array)$user->roles; $fields['billing']['customer_type']['default'] = empty($roles) ? 'Guest' : $roles[0]; return $fields; } add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' ); This uses the function empty() to determine if the $roles array is null/empty/unset, and based on that will set it to 'Guest' or the role name as it did previously. Quote Link to comment https://forums.phpfreaks.com/topic/312460-how-to-auto-populate-custom-field-with-current-user-role/#findComment-1585824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.