根据 Woocommerce 中的运输方式向客户通知添加回复电子邮件

Add a reply email to customer notifications based on shipping methods in Woocommerce

所有从默认 woocommerce 电子邮件地址发送给客户的订单电子邮件,例如 webshop@shop.com,这没问题,但我想为这些电子邮件添加回复电子邮件地址,以便用户能够回复此自定义地址, 例如 reply@webshop.com.

我还想根据 $order 送货方式设置此回复电子邮件地址。如果送货方式是 'local_pickup_plus' 将回复地址设置为 reply1@webshop.com,否则设置 reply2@webshop.com.

我明白了,我可以用 woocommerce_email_headers 过滤器修改 headers,但仅限于发送给管理员的电子邮件。

add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 3);
function mycustom_headers_filter_function( $headers, $object, $order ) {
    if ($object == 'new_order') {
        $headers .= 'Reply-to: teszt@teszt.hu';
    }

    return $headers;
}

如何为客户电子邮件设置此项?

以下代码将允许您根据客户电子邮件通知的送货方式有条件地添加不同的回复电子邮件:

// Utility function to get the shipping method Id from order object
function wc_get_shipping_method_id( $order ){
    foreach ( $order->get_shipping_methods() as $shipping_method ) {
        return $shipping_method->get_method_id();
    }
}

// Add coditionally a "reply to" based on shipping methods IDs for specific email notifications
add_filter( 'woocommerce_email_headers', 'add_headers_replay_to_conditionally', 10, 3 );
function add_headers_replay_to_conditionally( $headers, $email_id, $order ) {
    // Avoiding errors
    if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) )
        return $headers;

    // The defined emails notifications to customer
    $allowed_email_ids = array('customer_on_hold_order', 'customer_processing_order', 'customer_completed_order');

    // Only for specific email notifications to the customer
    if( in_array( $email_id, $allowed_email_ids ) ) {
        // Local Pickup Plus shipping method
        if( wc_get_shipping_method_id( $order ) === 'local_pickup_plus' ){
            $headers .= "Reply-to: reply1@webshop.com". "\r\n"; // Email adress 1
        } 
        // Other shipping methods
        else {
            $headers .= "Reply-to: reply2@webshop.com". "\r\n"; // Email adress 2
        }
    }

    return $headers;
}

代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。