WooCommerce: Get/Edit Logged in Username @ Checkout

On a recent job, a client asked me to get the logged in username in WooCommerce. Interesting task! He needed to show that on the checkout form page and also on the order email. Actually, this is quite simple to do and can be used for several applications.

WooCommerce: how to get the current user username
WooCommerce: how to get the current username

PHP snippet: get logged in username and show it on the WooCommerce checkout page in a billing field


/**
 * @snippet       Get Logged In Username - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=552
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.5.2
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action('woocommerce_after_checkout_billing_form', 'bbloomer_get_logged_in_username');

function bbloomer_get_logged_in_username( $checkout ) {

$current_user = wp_get_current_user();
$getmemid = $current_user->user_login;

woocommerce_form_field( 'memb_id', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Label'),
'required' => 'true',
'placeholder' => __('Placeholder'),
), $getmemid);

}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

10 thoughts on “WooCommerce: Get/Edit Logged in Username @ Checkout

  1. It’s working. How do you get the field to be apart of the WooCommerce ‘Order Details?’

    1. Hello Dan, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  2. Thank you very much. Works perfectly

    1. Cheers ๐Ÿ™‚

  3. Thanks for the snippit. Is it possible to make the field read only?

    1. Hi Ian – thanks so much for your comment! Yes, the woocommerce_form_field() function has an attribute ‘custom_attributes’ that can be used to make the input readonly. Hope this helps!

  4. This still works great in the most recent version of Woo. One quick question I have is using this snippet, how can I define where it shows up in the form. Right now it puts it at the bottom of the checkout form. Is it possible to say put it after email address or in a specific order?

    Thanks!

    1. Hey Jamie, thanks so much for your comment! Take a look at https://businessbloomer.com/woocommerce-move-reorder-fields-checkout-page/, you could add that as a new field and then define the priority maybe ๐Ÿ™‚

  5. how do you get this added tot he email as well?

    1. Kelly, thanks so much for your comment! Maybe this video tutorial might help: https://businessbloomer.com/customize-emails-woocommerce/

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.