【问题标题】:Get Order Notes from a Woocommerce Order / WC_Order object?从 Woocommerce Order / WC_Order 对象获取订单备注?
【发布时间】:2017-09-13 18:20:15
【问题描述】:

我可以添加订单备注(私人备注):

$order->add_order_note($info_for_order);

但是当我试图通过以下方式获取某些页面中的值时:

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

它只是返回一个空数组。我用谷歌搜索了这个,我找不到这样做的方法。

【问题讨论】:

  • 试试$order->add_order_note($info_for_order, 1);

标签: php sql wordpress woocommerce orders


【解决方案1】:

订单备注(私人备注)仅在使用get_comments()功能时可用于后端。
如果您查看WC_Comments exclude_order_comments() 方法,您会看到前端查询已过滤有关私人订单注释...

所以转机就是构建一个自定义函数来获取私有订单备注:

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;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码已经过测试并且可以工作。


用法 (例如$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>';
}

【讨论】:

  • 很好,我什至不知道您如何获得该信息。哈哈
  • @RaymondGoldmanSeger 我是 woocommerce 数据库间谍……这就是原因。我认为也可以使用新方法扩展 WC_Order 类,但更复杂,必须在插件上完成。
【解决方案2】:

有 woo 文档https://docs.woocommerce.com/wc-apidocs/function-wc_get_order_notes.html

例子:

wc_get_order_notes([
   'order_id' => $order->get_id(),
   'type' => 'customer',
]);

结果

Array
(
    [0] => stdClass Object
        (
            [id] => 11
            [date_created] => WC_DateTime Object
                (
                    [utc_offset:protected] => 3600
                    [date] => 2019-03-21 11:38:51.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

            [content] => Hi, blah blah blah
            [customer_note] => 1
            [added_by] => admin
        )

)

【讨论】:

    【解决方案3】:

    还有其他获取订单备注的最佳方式。

    /**
     * Get all approved WooCommerce order notes.
     *
     * @param  int|string $order_id The order ID.
     * @return array      $notes    The order notes, or an empty array if none.
     */
    function custom_get_order_notes( $order_id ) {
        remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
        $comments = get_comments( array(
            'post_id' => $order_id,
            'orderby' => 'comment_ID',
            'order'   => 'DESC',
            'approve' => 'approve',
            'type'    => 'order_note',
        ) );
        $notes = wp_list_pluck( $comments, 'comment_content' );
        add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
        return $notes;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 1970-01-01
      • 2019-11-12
      • 2021-04-08
      • 1970-01-01
      • 2018-03-16
      • 2017-10-22
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多