在Woocommerce中根据客户处理条件有条件地添加帐户电子邮件

时间:2018-11-08 16:29:57

标签: php wordpress woocommerce hook-woocommerce email-notifications

因此,这更多是我的解决方案,但我想对此进行开放,以查看社区是否可以找到更好的替代方案或潜在地找到解决方案的用途。

我们的客户要求我们对订单创建时收到的电子邮件订单收据进行以下更改:收据应发给帐户持有人,并抄送帐单电子邮件(如果不同)

我们知道Woocommerce默认在结帐时仅根据设置的 billing_email 发送订单收据(客户处理),因此我开始寻找一种在帐户所有者电子邮件中添加附加费的方法以及。

我做了一些挖掘工作,并在Stackoverflow上找到了有关如何执行此操作的答案,并且所提出的解决方案利用了 woocommerce_email_recipient_customer_processing_order 内置函数。这种方法只会在“收件人”标头中添加电子邮件-不理想。它还不考虑可能重复发送到同一电子邮件地址,至少对于我们的服务器而言,这会导致电子邮件无法发送。没有布宜诺斯艾利斯。

下面的函数代表解决此问题的方法,其中我们调用WP Core函数 wp_get_current_user()来获取与用户关联的电子邮件,然后检查其是否与帐单电子邮件。

add_filter( 'woocommerce_email_headers', 'add_email_recipient', 10, 3);

function add_email_recipient($header, $email_id, $order) {
    // Only for "Customer Processing Emails"  email notifications
    if( ! ( 'customer_processing_order' == $email_id ) ) return;
    $curr_user = wp_get_current_user();
    $account_holder_email = $curr_user->user_email;
    $billing_email = $order->get_billing_email();
    $header ='';
    if ( $account_holder_email != $billing_email ) {
        $header .= 'Cc: '.$account_holder_email;
    }
    return $header;
}

该逻辑打算通过以下方式流动:

  • 调整woocommerce电子邮件标题
  • 如果电子邮件为“ customer_processing_order”,请继续
  • 获取当前用户的电子邮件
  • 获取订单中设置的帐单电子邮件
  • 分配当前用户电子邮件的抄送字段
  • 完成

据我所知,没有比这更简单的方法了,所以我将其发布在这里,希望看看其他人是否有一个更优雅的解决方案。上面的代码通过放置子主题 functions.php 来工作。

1 个答案:

答案 0 :(得分:1)

  

您无法在电子邮件通知挂钩上获取当前用户或当前用户ID。
  您首先需要从订单中获取客户ID ,然后可以获取WP_User对象以获取帐户电子邮件。

与客户处理订单电子邮件通知中的订单结算电子邮件不同时,有两种添加客户帐户电子邮件的方法:

1)将客户帐户电子邮件添加为其他收件人:

add_filter( 'woocommerce_email_recipient_customer_processing_order', 'add_customer_processing_order_email_recipient', 10, 2 );
function add_customer_processing_order_email_recipient( $recipient, $order ) {
    // Not in backend (avoiding errors)
    if( is_admin() ) return $recipient;

    if( $order->get_customer_id() > 0 ){

        // Get the customer WP_User object
        $wp_user = new WP_User($order->get_customer_id());

        if ( $wp_user->user_email != $order->get_billing_email() ) {
            // Add account user email  to existing recipient
            $recipient .= ','.$wp_user->user_email;
        }
    }

    return $recipient;
}

代码进入您的活动子主题(活动主题)的function.php文件中。应该可以。


2)将客户帐户电子邮件添加为抄送电子邮件地址:

add_filter( 'woocommerce_email_headers', 'add_cc_email_to_headers', 10, 3);
function add_cc_email_to_headers($header, $email_id, $order) {
    // Only for "Customer Processing Emails"  email notifications
    if( 'customer_processing_order' == $email_id ) {

        if( $order->get_customer_id() > 0 ){
            // Get the customer WP_User object
            $wp_user = new WP_User($order->get_customer_id());

            if ( $wp_user->user_email != $order->get_billing_email() ) {
                $header .= 'Cc: ' . utf8_decode($order->get_formatted_billing_full_name() . ' <' . $wp_user->user_email . '>') . "\r\n";
            }
        }
    }
    return $header;
}

代码进入您的活动子主题(活动主题)的function.php文件中。应该可以。

相关问题