0

How to show user meta field of the user who placed the order in the Woocommerce Order admin page?

add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<strong>Store number</strong>' . get_user_meta( $user_id, 'wpcf-store-number', true );
}

This is what I have so far. The user meta field is wpcf-store-number.

Stanley Tan
  • 433
  • 1
  • 7
  • 21
  • to get the **user Id** from The `WC_Order` object use `$order->get_customer_id()` or `$order->get_user_id()` so `get_user_meta( $order->get_user_id(), 'wpcf-store-number', true );` – LoicTheAztec Jul 30 '20 at 16:36
  • Nice to know there are dedicated functions for that. Learned something new today. Thanks @LoicTheAztec – Terminator-Barbapapa Jul 30 '20 at 16:39

1 Answers1

0

You need the user ID to retrieve user meta data, which is not something the action you are using provides you with. It only passes you the order object. So you will first have to check if the order was placed by a registered user and get the user from the order object. Then you can take the user ID and check for the existence of certain user meta and output it:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'user_meta_after_admin_order_billing_address', 10, 1 );
function user_meta_after_admin_order_billing_address( $order ) {
    if ( $user = $order->get_user() ) {
        if ( $store_number = get_user_meta( $user->ID, 'wpcf-store-number', true ) ) {
            printf( '<p><strong>Store number:</strong> %s</p>', $store_number );
        }
    }
}
Terminator-Barbapapa
  • 3,063
  • 2
  • 5
  • 16