Woocommerce电子邮件会根据用户角色通知其他收件人

时间:2019-04-02 16:45:46

标签: wordpress email woocommerce user-roles

在Woocommerce中,我试图将“新订单”电子邮件发送到其他电子邮件地址。其他电子邮件地址取决于用户的角色。

基于Adding a custom email recipient depending on selected custom checkout field value答案代码,对其进行更改,这是我的代码:

add_filter( 'woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2 );
function new_order_conditional_email_recipient( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient; // (Optional)

    // Get the order ID (retro compatible)
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    // 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( 'user_role1', $user_data->roles )  )
        $recipient .= ', email1@gmail.com';
    elseif ( in_array( 'user_role2', $user_data->roles )  )
        $recipient .= ', email2@gmail.com';

    return $recipient;
}

我似乎无法找出如何从订单中获取用户信息。

这是我现在尝试的方法,但是当我尝试放置另一个时,出现“内部服务器错误”。

因此,我基本上是想找出如何从下订单的用户那里获取字段。

1 个答案:

答案 0 :(得分:0)

对于基于用户角色的用户,请尝试以下操作:

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

    // Get an instance of the WP_User Object related to the order
    $user = $order->get_user();

    // Add additional recipient based on custom user roles
    if ( in_array( 'user_role1', $user->roles )  )
        $recipient .= ', email1@gmail.com';
    elseif ( in_array( 'user_role2', $user->roles )  )
        $recipient .= ', email2@gmail.com';

    return $recipient;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。