dougou2937 2017-07-16 02:10
浏览 94
已采纳

Woocommerce电子邮件通知收件人有条件地基于自定义字段

I have a checkout form with a custom field.

I would like to add an extra recipient to an order email based on the value in the custom field. The custom field is currently a drop down menu with only 3 options.

Below is the code I was able to piece together with some googling however this does not appear to work.

function sv_conditional_email_recipient( $recipient, $order ) {

    $custom_field = get_post_meta($orderid, 'custom_field', true);

    if ($custom_field == "Value 1") 
    {
        $recipient .= ', email1@gmail.com';
    } 
    elseif ($custom_field == "Value 2") 
    {
        $recipient .= ', email2@gmail.com';
    }
    elseif ($custom_field == "Value 3") 
    {
        $recipient .= ', email3@gmail.com';
    }
    return $recipient;
}

add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );

Any help is appreciated.

Thanks.

  • 写回答

1条回答 默认 最新

  • dsio68964998 2017-07-16 02:32
    关注

    Your problem comes from the $orderid that is not defined. Try this instead:

    add_filter( 'woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2 );
    function new_order_conditional_email_recipient( $recipient, $order ) {
        if ( ! is_a( $order, 'WC_Order' ) ) return $recipient; // (Optional)
    
        // Get the order ID (retro compatible)
        $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
    
        // Get the custom field value (with the right $order_id)
        $custom_field = get_post_meta( $order_id, 'custom_field', true );
    
        if ($custom_field == "Value 1") 
            $recipient .= ', email1@gmail.com'; 
        elseif ($custom_field == "Value 2") 
            $recipient .= ', email2@gmail.com';
        elseif ($custom_field == "Value 3") 
            $recipient .= ', email3@gmail.com';
    
        return $recipient;
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Code is tested and works on WooCommerce 2.6.x and 3+.

    This hook is targeting "new_order" email notification only


    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角