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 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类