Jump to content

How to auto populate custom field with current user role


JayX

Recommended Posts

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 by JayX
Link to comment
Share on other sites

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 by MadTechie
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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' );
 

Link to comment
Share on other sites

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' );

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.