【问题标题】:Avoid sending completed order status email notification for a specific user role避免为特定用户角色发送已完成的订单状态电子邮件通知
【发布时间】:2017-02-14 12:40:26
【问题描述】:

我有批发客户的用户角色 (wholesale_customer)。当我将订单标记为已完成时,会向客户发送通知。这对我的老客户来说没问题,但我想禁用/删除批发客户的通知。

到目前为止我得到了什么:

function do_not_send_some_email_notifications(WC_Emails $wc_emails) {
    $order = new WC_Order( $order_id );
    if ( $order->user_id > 0 ) {
    $user_id = $order->user_id;
    $get_user_data = get_userdata($user_id); 

    $user_roles = $get_user_data->roles;
        if (in_array('wholesale_customer', $user_roles)) {
            remove_action('woocommerce_order_status_completed_notification', array($wc_emails->emails['WC_Email_Customer_Completed_Order'], 'trigger'));

        }
    }
}
add_action('woocommerce_email', 'do_not_send_some_email_notifications');

我已经对此进行了测试,但它不起作用。

如果有人能指出我正确的方向,那就太好了。

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    更新 2:我终于找到了合适的钩子让它工作。我已经使用 woocommerce_order_status_completed 操作挂钩中挂钩的非常相似的自定义函数重新访问了您的代码。

    代码如下:

    function custom_conditional_email_notifications( $order_id ) {
        // Set HERE the targetted user role
        $targeted_user_role = 'wholesale_customer';
    
        // Get the order object, the user ID, and the user role.
        $order = wc_get_order($order_id);
        $user_id =  $order->get_user_id();
        $user_info = get_userdata($user_id);
    
        if ( in_array( $targeted_user_role, $user_info->roles ) && $user_id > 0 )
            remove_action( 'woocommerce_order_status_completed_notification', array(
                $wc_emails->emails['WC_Email_Customer_Completed_Order'],
                'trigger'
            ) );
    }
    add_action( 'woocommerce_order_status_completed', 'custom_conditional_email_notifications' );
    

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

    此代码已经过测试并且可以工作。

    【讨论】:

    • 感谢您的帮助。只有 Targeted_user_role 的第一位给出错误(解析错误:语法错误,意外'=')。代码中可能有小错误吗?
    • 再次感谢。当我测试此代码时,具有用户角色“wholesale_customer”的用户在订单标记为已完成时仍会收到通知。我也应该将“管理员”更改为“批发客户”吗?
    • 我已经尝试了新代码,但仍然无法正常工作。我已经用两个帐户(管理员和批发客户)进行了尝试。在这两种情况下,我都在将订单标记为完成后收到了通知。可能与另一个插件/代码段冲突?
    猜你喜欢
    • 2018-08-31
    • 2017-02-19
    • 2017-04-27
    • 2017-04-25
    • 2017-04-28
    • 1970-01-01
    • 2020-09-01
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多