Displaying user data

Most information you’ll want to put on the invoice is available as order data, for which you can follow this guide: Displaying a custom field.

In some cases however, data is only available from the WordPress user profile. Here’s how to retrieve that data. Do bear in mind that depending on your WooCommerce setup, it may be possible for customers to check out as a guest, and there won’t be a user profile. Also, everything that’s already available in the order itself should be retrieved from the order because it may be different from what’s in the user profile.

User meta is very similar to order meta, except that it’s stored in a different table under a different name. We need the user id from the order and the ‘meta key’ (field name) that is used to store the data.

In a custom template you can use the following code snippet:

<?php
$user_id = get_post_meta($this->order->id,'_customer_user',true);
if ( !empty($user_id) ) {
	$meta_key = '_custom_field'; // change this to your meta key / custom field name
	echo get_user_meta( $user_id, $meta_key, true );
}
?>

Or if you want to keep the default template and only add the data to it, you can use an action hook (read more about this here):

add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_custom_user_meta', 10, 2 );
function wpo_wcpdf_custom_user_meta ($template_type, $order) {
    $user_id = get_post_meta($order->id,'_customer_user',true);
    if ( !empty($user_id) ) {
        $meta_key = '_custom_field'; // change this to your meta key / custom field name
        $custom_field = get_user_meta( $user_id, $meta_key, true );
        ?>
        <tr class="custom-user-meta">
            <th>Custom Field:</th>
            <td><?php echo $custom_field; ?></td>
        </tr>
        <?php
    }
}

For a more advanced use, you can also load the complete user object:

<?php
$user_id = get_post_meta($this->order->id,'_customer_user',true);
if ( !empty($user_id) ) {
    $user = get_user_by( 'id', $user_id );
    // do whatever you wish with the $user object here
}
?>