Get the customer user_id in Woocommerce email footer template

Loading

You can’t get the current user in woocommerce email templates, but you can get the customer user ID from the Order.

To get the customer User ID, you can set and get some data in the $GLOBAL variable:
Set the data – On your active theme function.php file:

add_action( 'woocommerce_email_header', 'wc_email_user_id', 10, 2 );
function wc_email_user_id( $email_heading, $email ) {
    // Set global variable data: user ID and Order ID
    $GLOBALS['emails_custom_data'] = array(
        'user_id' => get_post_meta( $email->object->ID, '_customer_user', true ), // Set the user ID
        'order_id' => $email->object->ID, // Set the Order ID
    );
}

Get the data – On your template file just after:

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

## --- Your custom Code start here --- ##

$refNameGlobalsVar = $GLOBALS; // Get the data
$custom_data = $refNameGlobalsVar['emails_custom_data']; // Set the data in a variable

$user_id = $custom_data['user_id']; // Get the user ID
$order_id = $custom_data['order_id']; // Get the order ID (in case of…)

// Testing output
echo '<p>The user ID is '.$user_id.' | From the Order ID: '.$order_id.'</p>';

## --- End of custom code --- ##

?>

About Post Author