Email recipients based on Shipping method Id for Woocommerce new order notification

末鹿安然 提交于 2019-12-13 10:35:51

问题


In Woocommerce, I would like to have a new order email sent to specific persons based on the selected shipping method ID.

Right now, I am only able to have every email address added to the new order email, but ultimately want to have the email be sent to one specific email based on the shipping method Id selected by the customer.

Here is my code coming from this answer that works for postal code:

add_filter( 'new_order' , 'so_26429482_add_recipient', 20, 2 );

function so_26429482_add_recipient( $email, $order ) {
    $additional_email = "somebody@somewhere.net";
    if( $order->shipping_postcode == "90210" ){
        $email = explode( ',', $email );
        array_push( $email, $additional_email );
    }
    return $email;
}

Instead I should need to target the shipping method Id.

My two shipping methods Ids are flat_rate:8 and flat_rate:9.

Does anyone know how to do this?


回答1:


Update 2 - Your code is outdated as new_order hook doesn't exit anymore.

Is not possible to remove the default email set in new order email notification. But we can add additional email recipient to new order email notification conditionally based on the order shipping method Id:

add_filter( 'woocommerce_email_recipient_new_order', 'new_order_additional_recipients', 20, 2 );
function new_order_additional_recipients( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    // Set Below your email adresses
    $email1 = 'name1@domain.com';
    $email2 = 'name2@domain.com';

    // Get the shipping method Id
    $shipping_items = $order->get_items('shipping');
    $shipping_item  = reset($shipping_items);
    $shipping_method_id  = $shipping_item->get_method_id() . ':';
    $shipping_method_id .= $shipping_item->get_instance_id();

    // Adding recipients conditionally
    if ( 'flat_rate:8' == $shipping_method_id )
        $recipient .= ',' . $email1;
    elseif ( 'flat_rate:9' == $shipping_method_id )
        $recipient .= ',' . $email2;

    return $recipient;
}

Code goes in function.php file of your active child theme (or active theme). tested and works.



来源:https://stackoverflow.com/questions/51768624/email-recipients-based-on-shipping-method-id-for-woocommerce-new-order-notificat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!