我正在尝试在Woocommerce向订单添加订单注释时触发功能。我已经完成了我的功能及其功能的测试。但是,只有在添加订购单时,才是正确的触发时间。
一家跟踪公司将跟踪代码添加到Woocommerce订单中,并将其标记为完成。我曾尝试使用订单状态更改操作钩子和电子邮件操作钩子,但发现在末尾添加了订单注释,因此需要通过钩子来捕获它。
这是我的代码:
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");
}
任何帮助将不胜感激。
P.S Woocommerce在今年最近似乎也贬值了很多。
答案 0 :(得分:0)
您可以使用woocommerce_order_note_added
操作:
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...
}