简体   繁体   中英

Changing WooCommerce e-mail recipient

How can I override the existing functionality in WooCommerce e-mail classes in order to change the recipient of an e-mail in my theme?

The class responsible for sending the e-mail I need to switch the recipient for is WC_Email_Cancelled_Order . Instead of sending it to a specified number of admin e-mails, I would like this to instead send the e-mail to the customer that the order has been cancelled.

In WC_Email_Cancelled_Order I figure that in the constructor on line 42 it sets the recipient.

Am I supposed to use the actions defined on line 35 and 36 in order to do this, instead? If so, how would I go about doing that?

edit:

I ended up doing the following in order to achieve what I'm trying to do

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

You can filter the recipient via the woocommerce_email_recipient_$email_id filter and supply a comma-separated list of emails.

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

Edit

With the advent of WooCommerce 2.7, $order->billing_email (and in fact, all order attributes) have been deprecated in favor of get_something() style methods. So to modify my answer for back and forwards compatibility I've added a method_exists() test:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM