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

标签 php wordpress loops woocommerce orders

我只想手动记录订单记录。我读到了有关它们的内容,发现它们是“内部”和“客户”。

基于Display last WooCommerce admin order note in customers order history答案代码,这是我的尝试:

$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>';
//} 

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

enter image description here

最佳答案

来自 wc_get_order_notes 的参数之一是类型

  • 内部用于管理和系统注释
  • 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 ) {     

关于php - 在 WooCommerce 中仅获取管理员手动订单注释(不是 "system"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66921770/

相关文章:

javascript - 我如何从对象(wordpress)读取自定义字段

php - Paypal IPN 捐赠

audio - XAudio2,启动/停止循环?

android - 如何在小部件中循环文本?

python - 了解 python 字典的顺序

php - 使用 htaccess 在 url 中使用数据库中的数据

php - 如何在 PHP 中转义这个 JavaScript 变量

php - 未从数据库 PHP LongBlob 接收图像

php - 如何将多字字符串作为变量传递给php中的url

css - 更改菜单底部边框的颜色