我认为可以通过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;
}
但是,这假定收件人是单个字符串(如果是一个数组,它将杀死 所有 收件人,而不仅仅是孩子......尽管默认情况下新订单电子邮件被发送到帐单电子邮件地址。
另外,请注意,我根本没有对此进行测试,因此您的里程可能会有所不同。