wordpress - 如何阻止按角色发送电子邮件?

时间:2015-04-27 13:13:31

标签: wordpress email woocommerce

我有一个教育Wordpress网站,学生可以在其中扮演角色'孩子的角色。和成年人有角色“订阅者”。我需要阻止发送给孩子的电子邮件'用户(通过Woocommerce - 但我认为它们是通过Wordpress中的邮件功能发送的)。

我可以在functions.php中添加一行来阻止邮件发送到特定角色吗?

提前致谢

玛利亚

1 个答案:

答案 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;
}

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

另外,请注意我根本没有测试过,所以你的里程可能会有所不同。