douren0558 2018-08-16 13:05
浏览 123
已采纳

在Woocommerce中的电子邮件订单总计表中插入自定义字段值作为新行

I'm helping my mother create her website for her store i Denmark. It has gone okay, but now i'm stuck with a problem. I need to be able to make a custom field in the order complete email, that displays a track & trace number, i've inserted on the admin order page. Everything i've done up until now, haven't worked, so therefore i seek your help.

I've added a custom field already called Track & Trace Pakkenr. (see screenshot 1) But the problem is getting this in the order complete email, under shipping (forsendelse in danish, see screenshot 2)

I am also a complete and utter beginner in coding so if any of you can give any help or advice, please make it almost foolproof to follow.

Here are the screenshot 1 and screenshot 2.

  • 写回答

3条回答 默认 最新

  • dongpao1873 2018-08-16 13:46
    关注

    To get this order custom field value displayed in order totals table on email notifications, use the following:

    add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
    function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ){
        // Only on emails notifications
        if( is_wc_endpoint_url() ) return $total_rows; // Exit
    
        $tracking_label = 'Track & Trace Pakkenr.'; // The tracking label name
        $tracking_value = $order->get_meta( $tracking_label ); // Get the tracking value (custom field).
    
        if( empty($tracking_value) ) return $total_rows; // Exit
    
        $new_total_rows  = array(); // Initializing
    
        // Loop through total rows
        foreach( $total_rows as $key => $value ){
            if( 'payment_method' == $key && ! empty($tracking_value) ) {
                $new_total_rows['tracking_parcel'] = array(
                    'label' => $tracking_label,
                    'value' => $tracking_value,
                );
            }
            $new_total_rows[$key] = $total_rows[$key];
        }
    
        return sizeof($new_total_rows) > 0 ? $new_total_rows : $total_rows;
    }
    

    Code goes in function.php file of your active child theme (or active theme). tested and works.

    You will get something like:

    enter image description here

    From this:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题