2015-04-27 84 views
0

我有一个教育的Wordpress网站,学生的角色是“孩子”,成年人的角色是“订阅者”。我需要防止电子邮件被发送给'孩子'用户(通过Woocommerce - 但我认为他们通过WordPress的邮件功能发送)。wordpress - 我怎样才能阻止角色发送电子邮件?

是否有一条线我可以在functions.php中添加以阻止邮件被发送到特定角色?

在此先感谢

玛丽亚

+0

你可以使用get_users('role');.如果用户是用户发送邮件 – vel

+0

什么类型的电子邮件正在发送给您不想发送的“子”用户? – maksbd19

+0

感谢您的反馈。订阅和订购电子邮件目前用于所有用户角色,但我不希望任何电子邮件发送给儿童。 –

回答

0

我认为有可能通过在get_recipient() method过滤器来调整电子邮件收件人。

/** 
* get_recipient function. 
* 
* @return string 
*/ 
public function get_recipient() { 
    return apply_filters('woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object); 
} 

我们以新订单电子邮件为例。下面是它的trigger()方法:

/** 
* trigger function. 
* 
* @access public 
* @return void 
*/ 
function trigger($order_id) { 
    if ($order_id) { 
     $this->object  = wc_get_order($order_id); 
     $this->find['order-date']  = '{order_date}'; 
     $this->find['order-number'] = '{order_number}'; 
     $this->replace['order-date'] = date_i18n(wc_date_format(), strtotime($this->object->order_date)); 
     $this->replace['order-number'] = $this->object->get_order_number(); 
    } 
    if (! $this->is_enabled() || ! $this->get_recipient()) { 
     return; 
    } 
    $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments()); 
} 

具体

if (! $this->is_enabled() || ! $this->get_recipient()) {

它说,如果没有收件人,则电子邮件将不会发送。另外$this->object = wc_get_order($order_id);告诉我们$order对象被传递给get_recipient_$id过滤器。

新订单电子邮件的ID是“customer_completed_order”,如电子邮件的class constructor所示。

SO,把所有的一起,我们可以筛选新订单电子邮件,收件人:

add_filter('so_29896856_block_emails', 'woocommerce_email_recipient_customer_completed_order', 10, 2); 
function so_29896856_block_emails($recipient, $order) { 
    if(isset($order->customer_user)){ 
     $user = new WP_User($customer_user); 
     if (in_array('child', (array) $user->roles)) { 
      $recipient = false; 
     } 
    } 
    return $recipient; 
} 

然而,这种假设收件人是一个字符串(如果数组它会杀死所有收件人,而不只是孩子......但默认情况下,新的订单电子邮件发送到开票电子邮件地址。

另外,请注意,我没有测试这一点,所以你的里程可能会有所不同

相关问题