duanfu5239 2017-04-18 03:44
浏览 388
已采纳

从Woocommerce订单/ WC_Order对象获取订单备注?

I can add an order note (private note) with:

$order->add_order_note($info_for_order);

But when I tried to get the values in some page with:

get_comments(['post_id' => $order_id])
// or
$order_object->get_customer_order_notes()

It simply returns an empty array. I googled this and i can't find the method to do it.

  • 写回答

2条回答 默认 最新

  • douchu4048 2017-04-18 05:15
    关注

    Order notes (private note) are only available for backend when using get_comments() function.
    If you look at WC_Comments exclude_order_comments() method you will see that front end queries are filtered regarding private order notes…

    So the turn around is to build a custom function to get the private Order notes:

    function get_private_order_notes( $order_id){
        global $wpdb;
    
        $table_perfixed = $wpdb->prefix . 'comments';
        $results = $wpdb->get_results("
            SELECT *
            FROM $table_perfixed
            WHERE  `comment_post_ID` = $order_id
            AND  `comment_type` LIKE  'order_note'
        ");
    
        foreach($results as $note){
            $order_note[]  = array(
                'note_id'      => $note->comment_ID,
                'note_date'    => $note->comment_date,
                'note_author'  => $note->comment_author,
                'note_content' => $note->comment_content,
            );
        }
        return $order_note;
    }
    

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

    This code is tested and works.


    Usage (for example the $order_id = 6238 ):

    $order_id = 6238;
    $order_notes = get_private_order_notes( $order_id );
    foreach($order_notes as $note){
        $note_id = $note['note_id'];
        $note_date = $note['note_date'];
        $note_author = $note['note_author'];
        $note_content = $note['note_content'];
    
        // Outputting each note content for the order
        echo '<p>'.$note_content.'</p>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值