Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Woocommerce电子邮件页脚模板中获取客户用户\u id_Php_Wordpress_Woocommerce_Userid_Email Notifications - Fatal编程技术网

Php 在Woocommerce电子邮件页脚模板中获取客户用户\u id

Php 在Woocommerce电子邮件页脚模板中获取客户用户\u id,php,wordpress,woocommerce,userid,email-notifications,Php,Wordpress,Woocommerce,Userid,Email Notifications,我试图在woocommerce自定义电子邮件模板中获取当前用户id,但它返回0。有人知道如何在wordpress的电子邮件模板中获取用户id吗。在任何php模板中使用此代码时,我都会获得用户id 下面是我的代码: <?php global $current_user; get_currentuserinfo(); echo '<h4>Your code:</h3>' . $current_user->ID . ''; ?>

我试图在woocommerce自定义电子邮件模板中获取当前用户id,但它返回0。有人知道如何在wordpress的电子邮件模板中获取用户id吗。在任何php模板中使用此代码时,我都会获得用户id

下面是我的代码:

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

以下是完整的模板代码链接:

我认为您应该使用wordpress挂钩,就像您在电子邮件模板中已经使用的那样:

apply_filters( 'woocommerce_email_footer_text', get_option( 'woocommerce_email_footer_text' )
而不是像这样直接写

global $current_user;
get_currentuserinfo();
echo '<h4>Your code:</h3>' . $current_user->ID . '';
global $current_user;
apply_filters( 'woocommerce_custom_template_email', $current_user->ID );
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;
}
当负责发送电子邮件的电子邮件功能被触发时,您可以使用如下添加过滤器

global $current_user;
get_currentuserinfo();
echo '<h4>Your code:</h3>' . $current_user->ID . '';
global $current_user;
apply_filters( 'woocommerce_custom_template_email', $current_user->ID );
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;
}
也许通过这种方式,您可以获取当前用户id。也可以让舒尔知道您正在尝试获取登录用户的当前用户id

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

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

  • 在活动主题
    function.php
    文件上设置数据:
add_action('woocommerce_email_header','wc_email_user_id',10,2);
功能wc_email_user_id($email_heading,$email){
//设置全局变量数据:用户ID和订单ID
$GLOBALS['emails\u custom\u data']=数组(
'user\u id'=>get\u post\u meta($email->object->id,'u customer\u user',true),//设置用户id
'order_id'=>$email->object->id,//设置订单id
);
}
  • 获取模板文件上的数据,然后:
if(!defined('ABSPATH')){
exit;//如果直接访问,则退出
}
##---您的自定义代码从这里开始--##
$refnamelobalsvar=$GLOBALS;//获取数据
$custom_data=$refnamelobalsvar['emails_custom_data'];//在变量中设置数据
$user_id=$custom_data['user_id'];//获取用户ID
$order_id=$custom_data['order_id'];//获取订单ID(如果是…)
//测试输出
回显“用户ID为“$user\u ID”。”来自订单ID:“.$Order\u ID”。

”; ##---自定义代码结束----## ?>

此代码经过测试并运行

只需一个函数就可以获取id,因此您不需要全局设置;变量和get_currentuserinfo()函数。只需使用get\u current\u user\u id()。如果它也不起作用,那么您需要提供该模板函数的完整代码。可能是您在该代码的另一部分中做错了什么。@Elvin85 ok尝试一下。@Elvin85返回0。我现在正在添加完整的文件代码。@Elvin85这里是代码链接,看起来还可以。这个模板文件在哪里?在woocommerce/templates中还是在您的主题/woocommerce/templates中?可能是因为你把文件弄错了。例如,尝试将“您的代码”更改为“您的代码”,并确保更改对发送的邮件产生影响。。。还有一件事:如果你称它为服务器端(使用CURL,CRON f.e.),它当然不会给出任何用户ID。