Home » WooCommerce: Get/Edit Logged in Username @ Checkout

WooCommerce: Get/Edit Logged in Username @ Checkout

by Tutor Aspire

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        Get tutoraspire.com FREE
 * @sourcecode    https://tutoraspire.com/?p=552
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 3.5.2
 * @donate $9     https://www.tutoraspire.com
 */

add_action('woocommerce_after_checkout_billing_form', 'tutoraspire_get_logged_in_username');

function tutoraspire_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);

}

You may also like