【问题标题】:Custom message on completed order status email notification only for customer user role仅针对客户用户角色的已完成订单状态电子邮件通知的自定义消息
【发布时间】:2017-02-19 23:52:12
【问题描述】:

我想从 this answer 改进此代码,以便仅向客户显示已完成订单状态电子邮件的消息,而不是其他用户角色(如订阅者等...)。

代码如下:

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

我怎样才能实现它?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

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

    代码如下:

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

    此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

    此代码经过测试且功能齐全。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-31
      • 2019-02-18
      • 2017-04-28
      • 2016-12-22
      • 2020-09-01
      • 2019-03-12
      • 2021-09-13
      • 2020-11-08
      相关资源
      最近更新 更多