0

I am trying to fire a function when Woocommerce adds an order note to an order. I am done with testing with my function and its works well. But only the right time to fire it is when an order note is added.

A tracking company adds tracking code to Woocommerce order notes and marks it complete. I have tried using order status changed action hook as well as email action hook but found out the order notes are added at the end so need to catch it via hook.

This is my code:

add_action( 'woocommerce_email_order_meta', 'tracking_add_order_notes_to_completed_email', 10 );
function tracking_add_order_notes_to_completed_email() {
        //do something
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$order_id = $order->get_id();   
global $wpdb;   
    
$comment_info = $wpdb->get_results("SELECT *  FROM `wp_comments` WHERE `comment_post_ID` = " .$order_id. " AND `comment_content` REGEXP 'Tracking ID'"); 
// display the results

    foreach($comment_info as $info) {
$tracking_id = $info->comment_content;
$tracking_id_sanitized = str_ireplace("Tracking ID : ","",$tracking_id);
    }
            
$meta_exist = metadata_exists('post', $order_id, '_wcst_order_trackno');
        $meta_val = get_post_meta( $order_id, '_wcst_order_trackno' );
    
        if($meta_exist == 1){
            
            if(!empty($meta_val[0]))
            {


            }else{
                $trno = $tracking_id_sanitized;
                update_post_meta( $order_id, '_wcst_order_trackno', $trno, '');
            }
        
        }else{
            
            $trno = $tracking_id_sanitized;
            add_post_meta( $order_id, '_wcst_order_trackno', $trno);
        }
    
    
error_log('['.date("F j, Y, g:i a e O").']'."This hook just got triggered"."\n", 3, $_SERVER['DOCUMENT_ROOT'] . "/log/my-errors.log");
    }

Any help would be greatly appreciated.

P.S Woocommerce also seems to have depreciate many action hooks recently in this year.

1 Answers1

0

You can use the woocommerce_order_note_added action:

add_action( 'woocommerce_order_note_added', 'my_function_after_order_note_was_added', 10, 2 );
function my_function_after_order_note_was_added ( $comment_id, $order ) {
    //Your code goes here...
}
Terminator-Barbapapa
  • 3,063
  • 2
  • 5
  • 16
  • Thank you so much for replying to my query. The life would be so easier if this hook works. But unfortunately it doesn't. Have tested it on 2 different installations with a PHP alert box and error-log function but this hook seems to be depreciated. – Abdul Samad Aug 21 '20 at 13:38
  • I have tried this hook (its actually a filter) it works but is fired before the note is added. Only a hook fired after the note added would work. ` add_action( 'woocommerce_new_order_note_data', 'action_function_name_7198', 999 , 2 ); function action_function_name_7198($commentdata, $order_id){ // action... error_log('['.date("F j, Y, g:i a e O").']'."Order Note hook triggered"."\n", 3, $_SERVER['DOCUMENT_ROOT'] . "/log/my-errors.log"); return $commentdata; } ` – Abdul Samad Aug 21 '20 at 13:40
  • It's not a filter, and it is called after an order note is added, as you can see in the [WooCommerce repo](https://github.com/woocommerce/woocommerce/blob/0f134ca6a20c8132be490b22ad8d1dc245d81cc0/includes/class-wc-order.php#L1741). This hook has only been available since WooCommerce 4.4.0, so make sure you are up to date. – Terminator-Barbapapa Aug 21 '20 at 13:47
  • I now see you did not mean my solution was a filter, but the solution you added in your last comment is a filter. My solution works if you update to WooCommerce 4.4.x. – Terminator-Barbapapa Aug 21 '20 at 14:33
  • You are right! Woocommerce added this new hook very recently. And thanks for the solution; You are awesome! It works for Woocommerce 4.4+. But unfortunately my shipping company not yet supports this version. I will need to work out a way to add this as a custom hook in 4.2.2 version. Any guide will be highly appreciated, – Abdul Samad Aug 21 '20 at 15:41