Php 仅针对客户用户角色的已完成订单状态电子邮件通知的自定义消息

Php 仅针对客户用户角色的已完成订单状态电子邮件通知的自定义消息,php,wordpress,woocommerce,orders,email-notifications,Php,Wordpress,Woocommerce,Orders,Email Notifications,我想改进这段代码,使其仅为客户显示已完成订单状态电子邮件上的消息,而不为其他用户角色(如订阅者等)显示消息 这是代码: add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 ); function completed_order_mail_message( $order ) { if ( empty( $order->get_used_coupons() ) &a

我想改进这段代码,使其仅为客户显示已完成订单状态电子邮件上的消息,而不为其他用户角色(如订阅者等)显示消息

这是代码:

add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 );
function completed_order_mail_message( $order ) {
    if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-completed' )
        echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
add_action('woocommerce_-email_-before_-order_-table','completed_-order_-mail_-message',20);
功能完成\u订单\u邮件\u消息($order){
如果(空($order->get_used_coups())&&&$order->post_status=='wc completed')
echo“获得20%的折扣”

感谢您购买此商品!请返回并使用代码“Back4More”在下次购买时获得20%的折扣!单击此处继续购物。

”; }
我怎样才能做到呢


感谢

要在电子邮件通知中为完整订单状态启用此自定义消息,仅为“客户”用户角色启用此自定义消息,您必须获取与订单相关的用户数据,才能获取用户角色。然后在条件语句中使用它

代码如下:

add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 );
function completed_order_mail_message( $order ) {

    // Getting order user data to get the user roles
    $user_data = get_userdata($order->customer_user);

    if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-completed' && in_array('customer', $user_data->roles) )
        echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
add_action('woocommerce_-email_-before_-order_-table','completed_-order_-mail_-message',20);
功能完成\u订单\u邮件\u消息($order){
//获取订单用户数据以获取用户角色
$user\u data=get\u userdata($order->customer\u user);
if(空($order->get_used_coups())&&&$order->post_status='wc completed'&&in_数组('customer',$user_data->roles))
echo“获得20%的折扣”

感谢您购买此商品!请返回并使用代码“Back4More”在下次购买时获得20%的折扣!单击此处继续购物。

”; }
这段代码位于活动子主题(或主题)的function.php文件或任何插件文件中

这段代码经过测试,功能齐全