我希望能够根据用户在订购时的角色来更改收到Woocommerce电子邮件通知的人员。
例如,如果用户以批发客户身份登录,则会通知其他电子邮件。
我已经找到了在使用woocommerce_email_recipient_new_order
挂钩完成新订单时如何更改它但我无法找到与失败或取消通知相关的任何挂钩。
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 );
有人可以帮忙吗?
答案 0 :(得分:1)
您已经使用的钩子是复合钩子:
woocommerce_email_recipient_{$this->id}
,其中{$this->id}
是WC_Email
ID,如new_order
。因此,您可以设置任何电子邮件ID,使其适用于所需的电子邮件通知。
下面您有“新订单”,“取消订单”和“失败订单”的3个挂钩,您可以将它们用于相同的挂钩功能。
在您的功能中,我删除了一些不必要的代码并完成了代码以获取与订单相关的客户数据(用户角色):
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;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
经过测试和工作。