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

我只是一个虾纸丫 提交于 2019-12-09 01:52:31

问题


I want to be able to change who receives the Woocommerce email notifications based on what role the user is when ordering.

For example, If the user is logged in as a Wholesale Customer then a different email will be notified.

I've found how to change it when a new order is complete using the woocommerce_email_recipient_new_order hook but i can't find any hooks related to the failed or cancelled notifications.

add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );


function sv_conditional_email_recipient( $recipient, $order ) {

    // Bail on WC settings pages since the order object isn't yet set yet
    // Not sure why this is even a thing, but shikata ga nai
    $page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
    if ( 'wc-settings' === $page ) {
        return $recipient; 
    }

    // just in case
    if ( ! $order instanceof WC_Order ) {
        return $recipient; 
    }


         if ( in_array( 'wholesale_customer', (array) $user->roles )  ) {
            $recipient .= ', shaun@example.com';
            return $recipient;
        }

    return $recipient;
}


add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );

Can anyone help please?


回答1:


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.



来源:https://stackoverflow.com/questions/47756330/add-recipients-based-on-user-role-to-failed-and-cancelled-woocommerce-emails

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