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 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 CustomizeWoo.com FREE
 * @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);

}

Share:

Leave a Reply