我有批发客户的用户角色 (wholesale_customer)。当我将订单标记为已完成时,会向客户发送通知。这对于我的老客户来说没问题,但我想禁用/删除批发客户的通知。
到目前为止我所得到的:
function do_not_send_some_email_notifications(WC_Emails $wc_emails) {
$order = new WC_Order( $order_id );
if ( $order->user_id > 0 ) {
$user_id = $order->user_id;
$get_user_data = get_userdata($user_id);
$user_roles = $get_user_data->roles;
if (in_array('wholesale_customer', $user_roles)) {
remove_action('woocommerce_order_status_completed_notification', array($wc_emails->emails['WC_Email_Customer_Completed_Order'], 'trigger'));
}
}
}
add_action('woocommerce_email', 'do_not_send_some_email_notifications');
我已经测试过这个,但它不起作用。
如果有人能指出我正确的方向,那就太好了。
谢谢。
最佳答案
更新2:我终于找到了正确的钩子(Hook)来让它发挥作用。我使用 Hook 在 woocommerce_order_status_completed
操作 Hook 中的非常相似的自定义函数重新访问了您的代码。
代码如下:
function custom_conditional_email_notifications( $order_id ) {
// Set HERE the targetted user role
$targeted_user_role = 'wholesale_customer';
// Get the order object, the user ID, and the user role.
$order = wc_get_order($order_id);
$user_id = $order->get_user_id();
$user_info = get_userdata($user_id);
if ( in_array( $targeted_user_role, $user_info->roles ) && $user_id > 0 )
remove_action( 'woocommerce_order_status_completed_notification', array(
$wc_emails->emails['WC_Email_Customer_Completed_Order'],
'trigger'
) );
}
add_action( 'woocommerce_order_status_completed', 'custom_conditional_email_notifications' );
代码位于事件子主题(或主题)的 function.php 文件中或任何插件文件中。
此代码经过测试并且可以工作。
关于php - 避免向特定用户角色发送已完成的订单状态电子邮件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42226465/