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 #

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 = $this->order->get_user_id();
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 = $order->get_user_id();
    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
    }
}

Load the WP user object #

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

<?php
$user = $this->order->get_user();
?>