Search code examples
phpwordpressloopswoocommerceorders

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


I want only to take manual order notes. I read about them and saw they are "internal" & "customer".

Based on Display last WooCommerce admin order note in customers order history answer code, here is my attempt:

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

This code gets all order notes. Is there a way to filter only manual added?

enter image description here


Solution

  • One of the parameters from wc_get_order_notes is type

    • internal is used for admin and system notes
    • customer is used for customer notes
    • leave empty for all

    However, adding this param solves only part of your problem. So I believe you can use added_by and if it is not equal to system, continue


    Note: in this example I used a static $order_id, replace with $order->get_id() if desired

    So you get:

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

    To disable bulk actions comments too:

    Replace

    foreach ( $order_notes as $order_note ) {
        // Added by
        $added_by = $order_note->added_by;
    
        // Content
        $content = $order_note->content;
        
        // Compare
        if ( $added_by != 'system' ) {  
    

    With (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' ) ) { 
    

    OR with (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 ) {