在 WooCommerce 中仅获取管理员手动订单备注(不是 "system")

Get only admin manual order notes (not "system") in WooCommerce

我只想做人工订单记录。我阅读了有关它们的信息,发现它们是“内部”和“客户”。

基于答案代码,这是我的尝试:

$order = wc_get_order( $post->ID );

// Get all order notes
$latest_notes = array();
    $latest_notes = wc_get_order_notes( array(
        'order_id' => $order->get_id(),
        'limit'    => 'all',
        'orderby'  => 'date_created_gmt',
        //'added_by_user' => 'true',
) );


//if ( !empty($payment_title) ) {
echo '<div class="wc-order-preview-order-note-container">';
echo '<div class="wc-order-preview-custom-note">';
echo '<h2 class="order-note">Order Notes:</h2><br>';
foreach ($latest_notes as $latest_note) {
    echo $latest_note->content."<br>";
    echo "<small>".$latest_note->date_created->date('j F Y - g:i:s')."</small><br>";
}       
    echo '</div>';
    echo '</div>';
//} 

此代码获取所有订单备注。 有没有办法只过滤手动添加的?

来自 wc_get_order_notes 的参数之一是 type

  • internal 用于管理和系统注释
  • customer用于客户备注
  • 全部留空

但是,添加此参数只能解决部分问题。所以我相信你可以使用added_by,如果它不等于system,继续


注意:在这个例子中我使用了静态$order_id,替换为$order->get_id() 如果需要

所以你得到:

$order_id = 2279;

// Get all order notes
$order_notes = wc_get_order_notes( array(
    'order_id' => $order_id,
    'orderby'  => 'date_created_gmt',
    'type'     => 'internal'
));

foreach ( $order_notes as $order_note ) {
    // Added by
    $added_by = $order_note->added_by;

    // Content
    $content = $order_note->content;
    
    // Compare
    if ( $added_by != 'system' ) {  
        // Date created - https://www.php.net/manual/en/function.date.php
        $date_created = $order_note->date_created->date( 'j F Y - g:i:s' );

        echo '<p>' . $added_by . '</p>';
        echo '<p>' . $content . '</p>';
        echo '<p>' . $date_created . '</p>';
    }
}

也要禁用批量操作评论:

替换

foreach ( $order_notes as $order_note ) {
    // Added by
    $added_by = $order_note->added_by;

    // Content
    $content = $order_note->content;
    
    // Compare
    if ( $added_by != 'system' ) {  

与 (PHP 8)

foreach ( $order_notes as $order_note ) {
    // Added by
    $added_by = $order_note->added_by;

    // Content
    $content = $order_note->content;
    
    // Compare and string NOT contains
    if ( $added_by != 'system' && ! str_contains( $content, 'Order status changed' ) ) { 

或与 (PHP 4, PHP 5, PHP 7, PHP 8)

foreach ( $order_notes as $order_note ) {
    // Added by
    $added_by = $order_note->added_by;
    
    // Content
    $content = $order_note->content;
    
    // Compare and string NOT contains
    if ( $added_by != 'system' && strlen( strstr( $content, 'Order status changed' ) ) == 0 ) {