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.