首页 > 解决方案 > 根据 Woocommerce 中的运输方式向客户通知添加回复电子邮件

问题描述

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

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

我发现,我可以使用woocommerce_email_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;
}

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

标签: phpwordpresswoocommerceemail-headersemail-notifications

解决方案


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

// 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 文件中。测试和工作。


推荐阅读