Add recipients based on user role to failed and cancelled WooCommerce emails

倾然丶 夕夏残阳落幕 提交于 2019-12-01 01:12:09

The hook you are already using is a composite hook: woocommerce_email_recipient_{$this->id}, where {$this->id} is the WC_Email ID like new_order. So you can set any email ID instead to make it work for the desired email notification.

Below You have the 3 hooks for "New Order", "Cancelled Order" and "Failed Order" that you can use for the same hooked function.

In your function, I have removed some unnecessary code and completed the code to get the customer data (the user roles) related to the order:

add_filter( 'woocommerce_email_recipient_new_order', 'user_role_conditional_email_recipient', 10, 2 );
add_filter( 'woocommerce_email_recipient_cancelled_order', 'user_role_conditional_email_recipient', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'user_role_conditional_email_recipient', 10, 2 );
function user_role_conditional_email_recipient( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    // Get the customer ID
    $user_id = $order->get_user_id();

    // Get the user data
    $user_data = get_userdata( $user_id );

    // Adding an additional recipient for a custom user role
    if ( in_array( 'wholesale_customer', $user_data->roles )  )
        $recipient .= ', shaun@example.com';

    return $recipient;
}

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

Tested and works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!