在 WooCommerce 中仅获取管理员手动订单备注(不是“系统”)

问题描述 投票:0回答:1

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

基于 在客户订单历史记录中显示最后的 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>';
//} 

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

enter image description here

php wordpress loops woocommerce orders
1个回答
3
投票

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 ) {     
© www.soinside.com 2019 - 2024. All rights reserved.