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

时间:2016-10-11 12:27:29

标签: php wordpress woocommerce orders email-notifications

我想从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>';
}

我怎样才能实现它?

由于

1 个答案:

答案 0 :(得分:3)

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

以下是代码:

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文件中或任何插件文件中。

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

相关问题