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 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥15 看一下OPENMV原理图有没有错误
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题