更改WooCommerce电子邮件收件人

时间:2016-03-09 18:59:17

标签: php wordpress email woocommerce

如何更改WooCommerce电子邮件类中的现有功能,以便更改主题中的电子邮件收件人?

负责发送我需要切换收件人的电子邮件的班级是WC_Email_Cancelled_Order。我希望这不是将其发送到指定数量的管理电子邮件,而是将电子邮件发送给客户,该订单已被取消。

WC_Email_Cancelled_Order中,我想在第42行的构造函数中设置了收件人。

我是否应该使用第35行和第36行定义的操作来执行此操作?如果是这样,我将如何做到这一点?

编辑:

我最终做了以下工作,以实现我想要做的事情

add_action( 'woocommerce_order_status_pending_to_cancelled_notification', 'hg_override_cancelled_email_recipient' );
add_action( 'woocommerce_order_status_on-hold_to_cancelled_notification', 'hg_override_cancelled_email_recipient' );

function hg_override_cancelled_email_recipient( $order_id ) {
    $cancelledEmail = new WC_Email_Cancelled_Order();

    if ( $order_id ) {
        $cancelledEmail->object                  = wc_get_order( $order_id );
        $cancelledEmail->find['order-date']      = '{order_date}';
        $cancelledEmail->find['order-number']    = '{order_number}';
        $cancelledEmail->replace['order-date']   = date_i18n( wc_date_format(), strtotime( $cancelledEmail->object->order_date ) );
        $cancelledEmail->replace['order-number'] = $cancelledEmail->object->get_order_number();
        $cancelledEmail->recipient               = $cancelledEmail->object->billing_email;
    }

    if ( ! $cancelledEmail->is_enabled() || ! $cancelledEmail->get_recipient() ) {
        return;
    }

    $cancelledEmail->send(
        $cancelledEmail->get_recipient(),
        $cancelledEmail->get_subject(),
        $cancelledEmail->get_content(),
        $cancelledEmail->get_headers(),
        $cancelledEmail->get_attachments()
    );
}

1 个答案:

答案 0 :(得分:1)

您可以通过woocommerce_email_recipient_$email_id过滤器过滤收件人,并提供以逗号分隔的电子邮件列表。

function so_35900307_email_recipient( $recipient, $order ){
   return $order->billing_email;
}
add_filter( 'woocommerce_email_recipient_cancelled_order', 'so_35900307_email_recipient', 10, 2 );

修改

随着WooCommerce 2.7的出现,$order->billing_email(事实上,所有订单属性)已被弃用,转而采用get_something()样式方法。因此,为了修改我的回传和兼容性的答案,我添加了method_exists()测试:

function so_35900307_email_recipient( $recipient, $order ){
   if( method_exists ( $order , 'get_billing_email' ) ){
        $recipient = $order->get_billing_email();
    } else {
        $recipient = $order->billing_email;
    }
    return $recipient;
}
add_filter( 'woocommerce_email_recipient_cancelled_order', 'so_35900307_email_recipient', 10, 2 );