在 Woocommerce 电子邮件页脚模板中获取客户 user_id

Joh*_*avo 3 php wordpress userid email-notifications woocommerce

我正在尝试在 woocommerce 自定义电子邮件模板中获取 current_user_id,但它返回 0。有谁知道如何在 wordpress 的电子邮件模板中获取 user_id。当我在任何 php 模板中使用此代码时,我都会获得用户 ID。

下面是我的代码:

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

这是完整的模板代码链接: email-template-code

Loi*_*tec 6

您无法在 woocommerce 电子邮件模板中获取当前用户,但可以从订单中获取客户用户 ID

要获取客户用户 ID,您可以在$GLOBAL变量中设置和获取一些数据:

  • 设置数据- 在您的活动主题function.php文件上:
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
    );
}
Run Code Online (Sandbox Code Playgroud)
  • 获取数据- 在您的模板文件上:
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 --- ##

?>
Run Code Online (Sandbox Code Playgroud)

此代码经过测试并有效