doujiu8826 2016-10-11 12:27
浏览 47
已采纳

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

I would like to improve this code from this answer, to display a message on completed order status emails only for customers, but not for other user roles (as subscribers, etc...).

Here is that code:

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>';
}

How can I achieve it?

Thanks

  • 写回答

1条回答 默认 最新

  • dsfb20227 2016-10-11 12:53
    关注

    To enable this custom message on email notification for complete order status and just for 'customer' user role, you have to get the user data related to the order, to get the user role. Then you will use it in your conditional.

    Here is the code:

    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>';
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and fully functional.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序