0

Hello I would to add the User_ID to WooCommerce order confirmation emails. I want my customers to be able to see their own USER ID number in the email that they receive after placing an order.

But I'm having a problem with my current code.

This is my current code:

<?php global $current_user;
  get_currentuserinfo();
  echo 'Your membership number: ' . $current_user->ID . "\n";
?>

But the problem is that this is not specific to the User ID of the actual order. This User ID grabs the ID of the logged in user, instead of the User ID of the user who placed the order.

It should maybe contain something like this instead?!:

$order->get_user_id()

I don't know how to change my current code.

Is someone able to modify my current code please?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Bec9999
  • 37
  • 1
  • 3
  • To display the User id BEFORE order table use: **`woocommerce_email_before_order_table`** instead of `woocommerce_email_after_order_table` – LoicTheAztec Mar 01 '19 at 07:43

2 Answers2

1

Try belo:

In WooCommerce 2.5, use get_post_meta() function this way:

$user_id = get_post_meta($order_id, '_customer_user', true);

In WooCommerce 3.0+ you can use the class WC_Order methods this way:

// Get the user ID from WC_Order methods
$user_id = $order->get_user_id(); // or $order->get_customer_id();
Jaydip Nimavat
  • 603
  • 9
  • 19
0
add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 );

function add_order_email_instructions( $order, $sent_to_admin ) {

     echo '<h2>USER ID: '.$order->get_user_id().'</h2>';
}
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • I tried it and it didn't seem to work. I was hoping that it would appear in the email template "email-order-details.php" and appear just above the table with the order details – Bec9999 Mar 01 '19 at 07:31
  • Please send a screenshot how u want – mujuonly Mar 01 '19 at 08:02