【问题标题】:wordpress - how can I block email sending by role?wordpress - 如何阻止按角色发送电子邮件?
【发布时间】:2015-04-27 13:13:30
【问题描述】:

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

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

提前致谢

玛丽亚

【问题讨论】:

  • 你可以使用 get_users('role');。如果用户是订阅者发送的邮件
  • 正在向child 用户发送您不想发送的电子邮件类型是什么?
  • 感谢您的反馈。订阅和订购电子邮件目前发送给所有用户角色,但我不希望任何电子邮件发送给儿童。

标签: wordpress email woocommerce


【解决方案1】:

我认为可以通过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 所示。

所以,将所有这些放在一起,我们可以过滤新订单电子邮件的收件人:

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

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

另外,请注意,我根本没有对此进行测试,因此您的里程可能会有所不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2022-12-21
    • 2016-04-06
    • 1970-01-01
    • 2014-11-21
    • 2023-03-26
    相关资源
    最近更新 更多