4

I am trying to get current_user_id in woocommerce custom email template but it returns 0. Does anyone know how to get user_id in email template in wordpress. I get the user id when I use this code in any php template.

Below is my code:

<?php global $current_user;
      get_currentuserinfo();
      echo '<h4>Your code:</h3>' . $current_user->ID . '';
?>

Here is complete template code link: email-template-code

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
John Bravo
  • 91
  • 1
  • 2
  • 10
  • Getting id is possible with single function, so you don't need global; variable and get_currentuserinfo() functions. Just use get_current_user_id(). If it doesn't work either, then you need to provide whole code of that template function. May be you did something wrong in another part of that code. – Elvin Haci Nov 15 '17 at 07:40
  • @Elvin85 ok trying it. – John Bravo Nov 15 '17 at 07:49
  • @Elvin85 it returns 0. I am adding full file code now. – John Bravo Nov 15 '17 at 07:54
  • @Elvin85 here is code link https://codeshare.io/GqXW1A – John Bravo Nov 15 '17 at 08:00
  • it seems ok there. where is this template file? in woocommerce/templates or in your_theme/woocommerce/templates? May be there is just mixing up and you edit wrong file. Try to change "Your Code" to "Your Codeeee" for example and make sure the change takes an affect in sent mails. ... And on more thing: if you call it serverside (with CURL, CRON f.e.) it would not give any user ID of course. – Elvin Haci Nov 15 '17 at 08:15

2 Answers2

7

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 --- ##

?>

This code is tested and works

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
1

I think you should use wordpress hooks like you already using in your email template:

apply_filters( 'woocommerce_email_footer_text', get_option( 'woocommerce_email_footer_text' )

Instead of directly writing like this

global $current_user;
get_currentuserinfo();
echo '<h4>Your code:</h3>' . $current_user->ID . '';

You could apply filter like this

global $current_user;
apply_filters( 'woocommerce_custom_template_email', $current_user->ID );

And when email function responsible for sending emails is triggered you use add_filter like this

add_filters( 'woocommerce_custom_template_email', 'custom_email_function' );
function custom_email_function($user_id) {
    global $current_user;
    $user_id = $current_user->ID;
    return $user_id;
}

Maybe in this way you can get current user id. Alse make shure that you are trying to get current user id for logged in users.

Tautvydas Banys
  • 113
  • 1
  • 9