WooCommerce: Display Logged in Username @ Checkout

On a recent job, a client asked me to display the logged-in username in WooCommerce. It sounded like a simple task, but it actually opened the door to some interesting use cases. He specifically wanted the username to appear on the Checkout page—perhaps to personalize the experience—and also within the Order confirmation email sent to the customer.

This kind of functionality can be useful in many scenarios: showing a personalized greeting, tagging orders with usernames for internal reference, or even building custom conditional logic based on the current user.

Luckily, WooCommerce gives us access to the current user object pretty easily. Whether you’re editing templates or using hooks, retrieving the logged-in username is a quick win that can help enhance the user experience and add a touch of personalization to the purchase flow.

Here’s a simple code snippet you can use to get the logged-in username in WooCommerce.

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

PHP Snippet: Show Username On The WooCommerce Checkout Page

/**
 * @snippet       Get Logged In Username - WooCommerce
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @testedwith    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */
 
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'),
      'placeholder' => __('Placeholder'),
   ), $getmemid );
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent 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. Follow @rmelogli

14 thoughts on “WooCommerce: Display Logged in Username @ Checkout

  1. Hi, thank you for this snippet. This works great on the checkout page, but it doesn’t come through on the order email… Is there a way to do this?

    1. Hi Matt, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hi, if i need to add a field or a title on the div “woocommerce-account-fields” when the user is not login in the chechout page what can i do?

    1. Hi Carlotta, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. 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

  4. Thank you very much. Works perfectly

    1. Cheers 🙂

  5. 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!

  6. 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 🙂

  7. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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