根据用户角色将收件人添加到失败和取消的 WooCommerce 电子邮件中
Add recipients based on user role to failed and cancelled WooCommerce emails
我希望能够根据用户在订购时的角色来更改接收 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 );
有人可以帮忙吗?
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.
下面 您有 "New Order"、"Cancelled Order" 和 "Failed Order" 的 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 文件或任何插件文件。
已测试并有效。
我希望能够根据用户在订购时的角色来更改接收 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 );
有人可以帮忙吗?
The hook you are already using is a composite hook:
woocommerce_email_recipient_{$this->id}
, where{$this->id}
is theWC_Email
ID likenew_order
. So you can set any email ID instead to make it work for the desired email notification.
下面 您有 "New Order"、"Cancelled Order" 和 "Failed Order" 的 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 文件或任何插件文件。
已测试并有效。