在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 .= ', [email protected]';
elseif ( in_array( 'user_role2', $user_data->roles ) )
$recipient .= ', [email protected]';
return $recipient;
}
我似乎无法找到如何从订单中获取用户信息。
这就是我现在尝试的,但是当我尝试放置另一个时,我得到了“内部服务器错误”。
所以我基本上试图找出如何从下订单的用户那里获取字段。
对于基于用户角色,请尝试以下操作:
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 .= ', [email protected]';
elseif ( in_array( 'user_role2', $user->roles ) )
$recipient .= ', [email protected]';
return $recipient;
}
代码位于活动子主题(或活动主题)的function.php文件中。它应该有效。